play

View as Markdown

play

Start a playback in the room. You can use the returned RoomSessionPlayback object to control the playback (e.g., pause, resume, setVolume and stop).

Parameters

params
objectRequired

Object containing the parameters of the method.

url
stringRequired

The url (http, https, rtmp, rtmps) of the stream to reproduce.

seekPosition
numberDefaults to 0

The starting timecode in milliseconds for playback.

volume
numberDefaults to 0

The playback volume from -50 to 50.

positions
VideoPositions

Positions to assign as soon as the playback starts. You can use the special keyword self to refer to the id of the playback.

layout
string

Layout to change to when the playback starts.

listen
object

Object of event listeners. See RoomSessionPlayback Events for a list of valid events. Example event: onStarted.

Returns

Promise<RoomSessionPlayback>

A promise that resolves to a RoomSessionPlayback object.

Example

In this example, we wait for a room to start and then play a video in that room. When a second member joins the roomSession, we stop the playback. This example assumes that there is a RoomSession already active and that members are joining the room.

1import { SignalWire } from "@signalwire/realtime-api";
2
3// Initialize the SignalWire client
4const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
5
6// Access the video client from the main client
7const videoClient = client.video;
8
9// Setup listener for when a room starts
10await videoClient.listen({
11 onRoomStarted: async (roomSession) => {
12 console.log("Room started", roomSession.displayName);
13
14 // play wait music
15 let roomPlayback = roomSession.play({
16 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
17 listen: {
18 onStarted: () => console.log("Playback started"),
19 onUpdated: (playback) => console.log("Playback updated", playback.state),
20 onEnded: () => console.log("Playback ended")
21 }
22 });
23 await roomSession.listen({
24 onMemberJoined: (member) => {
25 console.log("Member joined", member.name);
26 roomPlayback.stop();
27 },
28 onMemberLeft: (member) => {
29 console.log("Member left", member.name);
30 },
31 });
32 },
33 onRoomEnded: async (roomSession) => {
34 console.log("Room ended", roomSession.displayName);
35 }
36});