Developer Documentation

OdinMediaStartedStoppedEvent

type OdinMediaStartedStoppedEvent = (event: IOdinEvent<IOdinMediaStartedStoppedEventPayload>)

Discussion

The OdinMediaStartedStoppedEvent event is emitted on OdinRoom and OdinPeer instances whenever a remote peer started/stopped a media in the room. Usually, you would want to call the start or stop method on such media to start/stop the Opus decoder for audio playback.

Events are inheriting from IOdinEvent and provide an object described by IOdinMediaStartedStoppedEventPayload in the payload property of the event.

Event Scopes

  • IOdinRoomEvents
  • IOdinPeerEvents

Example

OdinMediaStartedStoppedEvent Example
import { OdinClient } from '@4players/odin';

const startOdin = async function (token: string) {
  // Authenticate using a token obtained externally and spawn a room instance
  const odinRoom = await OdinClient.initRoom(token);

  // Listen to media started events in the room and start decoding its voice packets
  odinRoom.addEventListener('MediaStarted', (event) => {
    console.log(`Peer ${event.payload.peer.id} added a new media stream to the room`);
    event.payload.media.start();
  });

  // Listen to media stopped events in the room and stop decoding its voice packets
  odinRoom.addEventListener('MediaStopped', (event) => {
    console.log(`Peer ${event.payload.peer.id} removed a media stream from the room`);
    event.payload.media.stop();
  });

  // Listen to activity events of started medias in the room (e.g. someone is talking)
  odinRoom.addEventListener('MediaActivity', (event) => {
    console.log(`Media ${event.payload.media.id} activity updated:`, event.payload.media.active);
  });

  // Join the room
  odinRoom.join();

  // Create a new audio stream for our default capture device and append it to the room
  navigator.mediaDevices.getUserMedia({ audio: true }).then((mediaStream) => {
    odinRoom.createMedia(mediaStream);
  });
};

startOdin('__YOUR TOKEN__').then(() => {
  console.log('Started ODIN');
});

Parameters

NameTypeDescription
eventIOdinEvent<IOdinMediaStartedStoppedEventPayload>