Developer Documentation
Our new developer documentation is now available. Please check it out!

PlaybackComponent

Switch to manual

public class PlaybackComponent : MonoBehaviour

Handles the Playback for received ODIN audio data.

Discussion

The PlaybackComponent is the heart of ODIN within Unity. It connects a Unity AudioSource with an ODIN room which runs on ODIN servers either hosted by 4Players or self-hosted.

Warning

Important: Don’t create this component yourself and don’t attach it in the Unity editor manually to components. Use the method AddPlaybackComponent from OdinHandler.Instance to create this component.

The component receives the audio stream of the server and “injects” it into the AudioSource .

Properties

NameDescription
PlaybackSourceThe Unity AudioSource component for playback
AutoDestroyAudioSourceOn true destroy the PlaybackSource in dispose to not leak (AudioSource) or false for manually manage sources
AutoDestroyMediaStreamOn true destroy the Media in dispose to not leak or false for manually manage stream
SampleRateThe playback Core
MuteThe Unity AudioSource mute property
RoomNameRoom name for this playback. Change this value to change the PlaybackStream by Rooms from the Client.
PeerIdPeer id for this playback. Change this value to change the PlaybackStream by RemotePeers in the Room.
MediaStreamIdMedia id for this playback. Change this value to pick a PlaybackStream by media id from peers Medias.
HasActivity

Public Methods

NameDescription
SetMediaInfo
GetOdinAudioStreamStats

Example

PlaybackComponent 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);
        }
    }
}