playAudio

View as Markdown

playAudio

Plays an audio file.

Parameters

params
objectRequired

Object containing the parameters for playing audio.

url
stringRequired

HTTP(s) URL to an audio resource to play.

volume
numberDefaults to 0

Volume value between -40dB and +40dB where 0 is unchanged.

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 an audio file. Once the audio file is finished playing, we hangup the call. We also listen for playback events.

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 an audio file. Listens for playback events. Ends the call after the audio file is finished playing.
13 call.answer();
14 await call.playAudio({
15 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
16 listen: {
17 onStarted: () => console.log("Started playing"),
18 onFailed: () => console.log("Failed to play"),
19 onUpdated: (event) => console.log("Updated playing", event.state),
20 onEnded: (event) => {
21 console.log("Ended playing", event.state);
22 // Hangup the call
23 call.hangup();
24 }
25 }
26 }).onStarted();
27 }
28});