Our new developer documentation is now available. Please check it out!
OdinHandler.OnMediaAdded
public MediaAddedProxy OnMediaAdded
Called on every Peer that created a media in the same room(s)
Value Type
Type | Description |
---|---|
MediaAddedProxy |
Discussion
Whenever a “user (in ODIN language it’s a peer) has joined a room and starts sending audio data (i.e. has activated the microphone) this event is triggered.
You need to respond to this event and call the AddPlaybackComponent function of the OdinHandler.Instance like shown in the example below.
Your callback function must have this structure. MediaAddedEventArgs contains information about the peer and the media added to the room:
public void OnMediaAdded(object sender, MediaAddedEventArgs eventArgs);
Whenever this callback is triggered, imagine a new player comes in the arena and the game master needs to give him a Walky Talky so that he can listen and talk to the other players.
Example
OnMediaAdded Example
using OdinNative.Odin;
using OdinNative.Odin.Room;
using OdinNative.Unity;
using OdinNative.Unity.Audio;
using UnityEngine;
public class MyOdinPeerManager : MonoBehaviour
{
private void AttachOdinPlaybackToPlayer(PlayerScript player, Room room, ulong peerId, int mediaId)
{
// Create the PlaybackComponent and AudioSource at the players gameObject
PlaybackComponent playback = OdinHandler.Instance.AddPlaybackComponent(player.gameObject, room.Config.Name, peerId, mediaId);
// Setup the Playback Component
playback.CheckPlayingStatusAsInvoke = true; // set checking status as MonoBehaviour.InvokeRepeating active
playback.PlayingStatusDelay = 1.0f; // (default 0f)
playback.PlayingStatusRepeatingTime = 0.3f; // (default 0.2f)
// Make the AudioSource a 3D source (use 0.0 if the volume should be the same regardless of position to the camera)
playback.PlaybackSource.spatialBlend = 1.0f; // set AudioSource to full 3D
}
public void OnMediaAdded(object sender, MediaAddedEventArgs eventArgs)
{
// Called if a peer has started to send audio
Room room = sender as Room;
Debug.Log($"ODIN MEDIA ADDED. Room: {room.Config.Name}, PeerId: {eventArgs.PeerId}, MediaId: {eventArgs.Media.Id}, UserData: {eventArgs.Peer.UserData.ToString()}");
// Use the User Data to to map the ODIN peer to the player in the network
CustomUserDataJsonFormat userData = CustomUserDataJsonFormat.FromUserData(eventArgs.Peer.UserData);
PlayerScript player = GetPlayerForOdinPeer(userData);
if (player)
{
// We have found a player, attach the ODIN audio to that player
AttachOdinPlaybackToPlayer(player, room, eventArgs.PeerId, eventArgs.Media.Id);
}
}
}