play

View as Markdown

play

Play one or multiple media in a Call and waits until the playing has ended.

The play method is a generic method for all types of media, see playAudio, playSilence, playTTS or playRingtone for more specific usages.

Parameters

params
objectRequired

Object containing the parameters for playing media.

playlist
VoicePlaylistRequired

A media playlist. See VoicePlaylist.

listen
object

Callback to listen for events. List of playback events can be found here. Example event: onStarted.

Returns

Promise<CallPlayback>

A promise that resolves to a CallPlayback object that you can use to view the current state and results of the play session.

Example

In this example, we dial a phone number and play a TTS message. Once the TTS message is finished playing, we then play silence on the call for 1 second and then play an audio file. Once the audio file is finished playing, we hangup the call.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 console.log("Call received");
12 // Answer the call and play a TTS message using the generic play method, then play silence for 1 second, then play an audio file.
13 call.answer();
14 const playlist = new Voice.Playlist({ volume: 1.0 })
15 .add(Voice.Playlist.TTS({
16 text: 'Welcome to SignalWire!'
17 }))
18 .add(Voice.Playlist.Silence({ duration: 1 }))
19 .add(Voice.Playlist.Audio({
20 url: 'https://cdn.signalwire.com/default-music/welcome.mp3'
21 }))
22 await call.play({
23 playlist: playlist,
24 listen: {
25 onStarted: () => console.log('Playback started!'),
26 onFailed: (playback) => console.log('Playback failed', playback.state),
27 onUpdated: (playback) => console.log('Playback state is:', playback.state),
28 onEnded: (playback) => {
29 console.log('Playback ended', playback.state);
30 // Hangup the call
31 call.hangup();
32 },
33 }
34 }).onStarted();
35 }
36});