Our new developer documentation is now available. Please check it out!
PlaybackComponent
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
The component receives the audio stream of the server and “injects” it into the AudioSource .
Properties
Name | Description |
---|---|
PlaybackSource | The Unity AudioSource component for playback |
AutoDestroyAudioSource | On true destroy the PlaybackSource in dispose to not leak (AudioSource) or false for manually manage sources |
AutoDestroyMediaStream | On true destroy the Media in dispose to not leak or false for manually manage stream |
SampleRate | The playback Core |
Mute | The Unity AudioSource mute property |
RoomName | Room name for this playback. Change this value to change the PlaybackStream by Rooms from the Client. |
PeerId | Peer id for this playback. Change this value to change the PlaybackStream by RemotePeers in the Room. |
MediaStreamId | Media id for this playback. Change this value to pick a PlaybackStream by media id from peers Medias. |
HasActivity |
Public Methods
Name | Description |
---|---|
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);
}
}
}