CallPlayback
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.
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({ project, token });
const call = await client.voice.dialPhone({
from: "+YYYYYYYYYY",
to: "+XXXXXXXXXX",
});
// Start a TTS playback
await call.playTTS({
text: "Welcome to SignalWire!",
listen: {
onStarted: () => console.log("Playback started"),
onUpdated: (playback) => console.log("Playback updated", playback.state),
onEnded: async (playback) => {
console.log("Playback ended", playback.state);
// Hangup the call
call.hangup();
},
onFailed: () => console.log("Playback failed")
}
}).onStarted();
Properties​
id​
The unique ID for this playback.
Syntax: CallPlayback.id()
Returns: string
Methods​
pause​
â–¸ pause(): Promise
<CallPlayback
>
Pauses the playback.
Returns​
Promise
<CallPlayback
>
A promise that is resolved only after the playback is paused.
Example​
const playback = await call.playAudio({
url: "https://cdn.signalwire.com/default-music/welcome.mp3",
}).onStarted();
await playback.pause();
ended​
â–¸ ended(): Promise
<CallPlayback
>
Waits for the playback to end.
Returns​
Promise
<CallPlayback
>
A promise that is resolved to CallPlayback
when the playback ends.
Example​
const playback = await call.playAudio({
url: "https://cdn.signalwire.com/default-music/welcome.mp3",
}).onStarted();
await playback.ended();
resume​
â–¸ resume(): Promise
<CallPlayback
>
Resumes the playback if it was paused.
Returns​
Promise
<CallPlayback
>
A promise that is resolved to CallPlayback
when the playback is resumed.
Example​
const playback = await call.playAudio({
url: "https://cdn.signalwire.com/default-music/welcome.mp3",
}).onStarted();
await playback.resume();
setVolume​
â–¸ setVolume(volume
): Promise
<CallPlayback
>
Changes the volume of the playback.
Parameters​
Name | Type | Description |
---|---|---|
volume | number | Volume value between -40dB and +40dB. |
Returns​
Promise
<CallPlayback
>
A promise that is resolved to CallPlayback
when the volume is changed.
Example​
const playback = await call.playAudio({
url: "https://cdn.signalwire.com/default-music/welcome.mp3",
}).onStarted();
await playback.setVolume(-20);
stop​
â–¸ stop(): Promise
<CallPlayback
>
Stops the playback.
Returns​
Promise
<CallPlayback
>
A promise that is resolved to CallPlayback
when the playback is stopped.
Example​
const playback = await call.playAudio({
url: "https://cdn.signalwire.com/default-music/welcome.mp3",
}).onStarted();
await playback.stop();
Events​
onStarted​
â–¸ CallCollect.listen({ onStarted: Callback }}
)
Emitted when the playback starts playing. Your event handler will receive an instance of CallPlayback
.
Parameters​
Name | Type | Description |
---|---|---|
playback | CallPlayback | The playback instance. |
onUpdated​
â–¸ CallCollect.listen({ onUpdated: Callback }}
)
Emitted when the playback is updated. Your event handler will receive an instance of CallPlayback
.
Parameters​
Name | Type | Description |
---|---|---|
playback | CallPlayback | The playback instance. |
onFailed​
â–¸ CallCollect.listen({ onFailed: Callback }}
)
Emitted when the playback fails to start. Your event handler will receive an instance of CallPlayback
.
Parameters​
Name | Type | Description |
---|---|---|
playback | CallPlayback | The playback instance. |
onEnded​
â–¸ CallCollect.listen({ onEnded: Callback }}
)
Emitted when the playback finishes playing. Your event handler will receive an instance of CallPlayback
.
Parameters​
Name | Type | Description |
---|---|---|
playback | CallPlayback | The playback instance. |