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

NameTypeDescription
PlaybackSourceAudioSourceThe Unity AudioSource component for playback
MuteBooleanThe Unity AudioSource mute property
MuteStreamBooleanThe Odin PlaybackStream underlying media stream calls
RoomNameStringRoom name for this playback. Change this value to change the PlaybackStream by Rooms from the Client.
PeerIdUInt64Peer id for this playback. Change this value to change the PlaybackStream by RemotePeers in the Room.
MediaIdUInt16Media id for this playback. Change this value to pick a PlaybackStream by media id from peers Medias.
AutoDestroyAudioSourceBooleanOn true destroy the PlaybackSource in dispose to not leak (AudioSource) or false for manually manage sources
AutoDestroyMediaStreamBooleanOn true destroy the in dispose to not leak or false for manually manage stream
OverrideSampleRateBooleanUse set RemoteSampleRate on false
SampleRateMediaSampleRateThe playback
HasActivityBoolean

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