Whether it’s as simple as playing music while your callers are on hold or greeting callers with a specific message, playing pre-recorded audio is an important part of any call logic. Plivo supports both MP3 and WAV file formats. The <Play> XML element is used to play an audio file (referenced by URL) to the caller.
Check out our server-side SDKs page and install the right helper based on the programming language you want to use.
Buy a Plivo phone number. A phone number is required to receive calls. You can buy a Plivo phone number in over 20+ countries through the Buy Numbers tab on your Plivo account UI. Check the Voice API coverage page for all the supported countries.
Use a web hosting service to host your web application. There are many inexpensive cloud hosting providers that you can use for just a few dollars a month. Follow the instructions of your hosting provider to host your web application.
Set up a Web Server
Let’s assume your web server is located at myvoiceapp.com. Below is a snippet to set up a route on your webserver. Let’s call it, play. Now when we send an HTTP request to myvoiceapp.com/play this route will be invoked. You will now have to configure this URL in your Plivo application.
Note: For PHP, the route will be myvoiceapp.com/play.php
Copy the relevant code below into a text file and save it. Let’s call it, play.
Note: Make sure to use the appropriate file extention for your code (e.g., play.py for Python).
Now that you have the code, you will need to expose your server to the public Internet. This way, Plivo will know where to find your app when a particular phone number is dialed. Moving forward, we will assume that your app is available at myvoiceapp.com.
fromflaskimportFlask,request,make_responseimportplivo,plivoxmlapp=Flask(__name__)@app.route('/play/',methods=['GET','POST'])defplay_xml():# Generate a Play XML with the details of audio file to play during the call
body="https://s3.amazonaws.com/plivocloud/Trumpet.mp3"r=plivoxml.Response()r.addPlay(body)printr.to_xml()returnResponse(str(r),mimetype='text/xml')if__name__=="__main__":app.run(host='0.0.0.0',debug=True)
varplivo=require('plivo');varexpress=require('express');varapp=express();app.set('port',(process.env.PORT||5000));app.all('/play/',function(request,response){// Generate a Play XML with the details of audio file to play during the callvarr=plivo.Response();r.addPlay("https://s3.amazonaws.com/plivocloud/Trumpet.mp3");console.log(r.toXML());response.set({'Content-Type':'text/xml'});response.send(r.toXML());});app.listen(app.get('port'),function(){console.log('Node app is running on port',app.get('port'));});
1
2
3
4
5
6
7
8
9
10
11
12
13
<?phprequire'vendor/autoload.php';usePlivo\Response;// Generate a Play XML with the details of the audio file to play on the call.$body="https://s3.amazonaws.com/plivocloud/Trumpet.mp3";$r=newResponse();// Add Play element$r->addPlay($body);Header('Content-type: text/xml');echo($r->toXML());?>
usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingRestSharp;usingPlivo.XML;usingNancy;namespaceplay_recorded{publicclassProgram:NancyModule{publicProgram(){Get["/play/"]=x=>{Plivo.XML.Responseresp=newPlivo.XML.Response();// Add Play tagresp.AddPlay("https://s3.amazonaws.com/plivocloud/Trumpet.mp3",newDictionary<string,string>(){});Debug.WriteLine(resp.ToString());varoutput=resp.ToString();varres=(Nancy.Response)output;res.ContentType="text/xml";returnres;};}}}
Give your application a name. Let’s call it Play Audio. Enter your server URL (e.g., http://myvoiceapp.com/play) in the Answer URL field and set the method as POST or GET. See our Application API docs to learn how to modify your application through our APIs.
Click on Create to save your application.
Assign a Plivo number to your app
Navigate to the Numbers page and select the phone number you want to use for this app.
Select Play Audio (name of the app) from the Plivo App dropdown list.
Click on Update to save.
If you don’t have a Plivo phone number, go to the Buy Number page to purchase a Plivo phone number.
Test it out
When you make a call to your Plivo number, Plivo will play the audio file found at https://s3.amazonaws.com/plivocloud/Trumpet.mp3. After the music is played, the call is automatically hung up. The XML instructions to Plivo are below.