Play Audio File on Call
About
Play a .mp3 or .wav file on an active phone call. For more information about playing an audio file onto a call, visit the full documentation.
Assumptions
- You have signed up for the Bandwidth voice & messaging APIs
- You are familiar with:
Steps
- Create call with callback URL -or- Receive Incoming Call
- Check if call is answered
- Play audio on call
Step 1 - Create outbound call with callback url.
Click here to learn more about creating an outbound call.
Create Call Parameters
Property | Description |
---|---|
from | A Bandwidth phone number on your account the call should come from (must be in E.164 format, like +19195551212). |
to | The number to call (must be either an E.164 formatted number, like +19195551212, or a valid SIP URI, like sip:someone@somewhere.com). |
callbackUrl | The full server URL where the call events related to the Call will be sent to. |
Example of creating a call with callbackUrl
POST https://api.catapult.inetwork.com/v1/users/{userId}/calls HTTP/1.1
Content-Type: application/json; charset=utf-8
Authorization: {apiToken:apiSecret}
{
"to" : "{toNumber}",
"from" : "{fromNumber}",
"callbackUrl" : "{callbackUrl}"
}
//This assumes you have already input your credentials.
client.Call.create({
from: "{toNumber}",
to: "{fromNumber}",
callbackUrl: "{callbackUrl}"
})
.then(function (id) {
console.log(id);
})
//This assumes you have already input your credentials.
var call = await client.Call.CreateAsync(new CreateCallData{
From = "{fromNumber}",
To = "{toNumber}"
CallbackUrl = "{callbackUrl}"
});
Console.WriteLine($"Created call with id {call.Id}");
## This assumes you have already input your credentials.
call = Bandwidth::Call.create({
:from => "{fromNumber}",
:to => "{toNumber}",
:callbackUrl => "{requestUrl}"})
puts call.id
Step 2 - Check if call is answered
In order to play an audio file on a call, the call must be answered. When the call is answered, Bandwidth will notify the callbackUrl with an eventType = "answer". Once we receive this callback, we can procede with playing the audio file.
Example of callback answer event
json response
POST /your_url HTTP/1.1
Content-Type: application/json; charset=utf-8
User-Agent: BandwidthAPI/v1
{
"eventType" : "answer",
"from" : "{toNumber}",
"to" : "{fromNumber}",
"callId" : "{callId}",
"callUri" : "{callUri}",
"callState" : "active",
"time" : "date"
}
Step 3 - Play audio file on call
Click here to learn more about playing an audio file in a call.
Play an audio file
Property | Description |
---|---|
fileUrl | The location of an audio file to play (WAV and MP3 supported). To STOP AUDIO FILE PLAYBACK send an empty string like: {"fileUrl": ""} |
Example of playing an audio file
POST https://api.catapult.inetwork.com/v1/users/{userId}/calls/{callId}/audio HTTP/1.1
Content-Type: application/json; charset=utf-8
Authorization: {apiToken:apiSecret}
{
"fileUrl": "{fileUrl}"
}
//Promise
client.Call.playAudioFile("{callId}", "{fileUrl}").then(function (res) {});
//Callback
client.Call.playAudioFile("{callId}", "{fileUrl}", function (err, res)
await client.Call.PlayAudioFileAsync("{callId}", "{fileUrl}");
call.play_audio({
:file_url => "{fileUrl}"
})