CallPlayback

View as Markdown

Represents a current or past playback in a call.

Obtain instances of this class by starting a Playback with one of the following methods:

Example

Playing a text-to-speech message and waiting for it to end before proceeding to the next instructions.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project, token });
4
5const call = await client.voice.dialPhone({
6 from: "+YYYYYYYYYY",
7 to: "+XXXXXXXXXX",
8});
9
10// Start a TTS playback
11await call.playTTS({
12 text: "Welcome to SignalWire!",
13 listen: {
14 onStarted: () => console.log("Playback started"),
15 onUpdated: (playback) => console.log("Playback updated", playback.state),
16 onEnded: async (playback) => {
17 console.log("Playback ended", playback.state);
18 // Hangup the call
19 call.hangup();
20 },
21 onFailed: () => console.log("Playback failed")
22 }
23}).onStarted();

Properties

id
string

The unique ID for this playback.

state
"playing" | "paused" | "finished" | "error"

The current state of the playback.

hasEnded
boolean

Whether the playback has ended. Returns true if the state is "finished" or "error".

Methods

pause

Pauses the playback.

Returns

Promise<CallPlayback>

A promise that is resolved only after the playback is paused.

Example

1const playback = await call.playAudio({
2 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
3}).onStarted();
4await playback.pause();

ended

Waits for the playback to end.

Returns

Promise<CallPlayback>

A promise that is resolved to CallPlayback when the playback ends.

Example

1const playback = await call.playAudio({
2 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
3}).onStarted();
4await playback.ended();

resume

Resumes the playback if it was paused.

Returns

Promise<CallPlayback>

A promise that is resolved to CallPlayback when the playback is resumed.

Example

1const playback = await call.playAudio({
2 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
3}).onStarted();
4await playback.resume();

setVolume

Changes the volume of the playback.

Parameters

volume
numberRequired

Volume value between -40dB and +40dB.

Returns

Promise<CallPlayback>

A promise that is resolved to CallPlayback when the volume is changed.

Example

1const playback = await call.playAudio({
2 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
3}).onStarted();
4await playback.setVolume(-20);

stop

Stops the playback.

Returns

Promise<CallPlayback>

A promise that is resolved to CallPlayback when the playback is stopped.

Example

1const playback = await call.playAudio({
2 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
3}).onStarted();
4await playback.stop();

Events

onStarted

  • CallPlayback.listen({ onStarted: Callback }})

Emitted when the playback starts playing. Your event handler will receive an instance of CallPlayback.

Parameters

playback
CallPlaybackRequired

The playback instance. See CallPlayback.

onUpdated

  • CallPlayback.listen({ onUpdated: Callback }})

Emitted when the playback is updated. Your event handler will receive an instance of CallPlayback.

Parameters

playback
CallPlaybackRequired

The playback instance. See CallPlayback.

onFailed

  • CallPlayback.listen({ onFailed: Callback }})

Emitted when the playback fails to start. Your event handler will receive an instance of CallPlayback.

Parameters

playback
CallPlaybackRequired

The playback instance. See CallPlayback.

onEnded

  • CallPlayback.listen({ onEnded: Callback }})

Emitted when the playback finishes playing. Your event handler will receive an instance of CallPlayback.

Parameters

playback
CallPlaybackRequired

The playback instance. See CallPlayback.