The <Play>
verb plays an audio file back to the caller. Twilio retrieves
the file from a URL that you provide.
The media URL is provided between <Play>
's opening and closing tags, as shown in the example below.
1const VoiceResponse = require('twilio').twiml.VoiceResponse;234const response = new VoiceResponse();5response.play('https://api.twilio.com/cowbell.mp3');67console.log(response.toString());
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Play>https://api.twilio.com/cowbell.mp3</Play>4</Response>
The <Play>
verb supports the following attributes that modify its behavior:
The loop
attribute specifies how many times the audio file is played.
The default behavior is to play the audio once. Specifying 0
will cause
the <Play>
verb to loop until either the call is hung up or 1000 iterations are performed.
The example below causes Twilio to play the audio from https://api.twilio.com/cowbell.mp3
10 times.
1const VoiceResponse = require('twilio').twiml.VoiceResponse;234const response = new VoiceResponse();5response.play({6loop: 107}, 'https://api.twilio.com/cowbell.mp3');89console.log(response.toString());
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Play loop="10">https://api.twilio.com/cowbell.mp3</Play>4</Response>
The digits
attribute lets you play DTMF tones during a call.
For example, if you need to test an IVR system, you can use this feature to simulate digits being pressed to navigate through the menu options.
Include w
to introduce a 0.5s
pause between DTMF tones. For example, 1w2
will tell Twilio to pause 0.5s before playing DTMF tone 2. To include a one-second pause, use ww
.
If you are dialing a phone number and need to play DTMF tones to enter the extension, you should use <Number>'s sendDigits attribute.
Twilio does not send its standard HTTP parameters when making requests to <Play>
URLs.
The TwiML example below shows the use of <Play>
and its digits
attribute. The value of the digits
attribute is www3
, which causes Twilio to pause for 1.5 seconds before playing the DTMF tone for 3
.
Twilio supports the following audio MIME types for audio files retrieved
by the <Play>
verb:
MIME type | Description |
---|---|
audio/mpeg | mpeg layer 3 audio |
audio/wav | wav format audio |
audio/wave | wav format audio |
audio/x-wav | wav format audio |
audio/aiff | audio interchange file format |
audio/x-aifc | audio interchange file format |
audio/x-aiff | audio interchange file format |
audio/x-gsm | GSM audio format |
audio/gsm | GSM audio format |
audio/ulaw | μ-law audio format |
You can't nest any verbs within <Play>
. You can nest <Play>
within a <Gather> verb, with one major exception - you can't play "digits" within a <Gather>
.
This TwiML document tells Twilio to download the cowbell.mp3
file and
play the audio to the caller.
We are going to test our IVR menu to make sure users can navigate properly.
We know that the length of the initial greeting and the menu number we need
to enter. We can add a few leading w
characters to add a pause. Each w
character tells Twilio to wait 0.5 seconds instead of playing a digit. This
lets you adjust the timing of when the digits begin playing to suit the phone
system you are dialing.
Cache-Control: no-cache
will ensure Twilio always checks if the file has changed, allowing your web server to respond with a new version or with a 304 Not Modified to instruct Twilio to use its cached version.<Play>
ing a file that is longer than 40 minutes can result in a dropped call. If you need to <Play>
a file longer than 40 minutes, consider splitting it up into smaller chunks.