OdinHandler.AddPlaybackComponent

public PlaybackComponent AddPlaybackComponent(string gameObjectTag, string roomName, ulong peerId, long mediaStreamId, bool autoDestroySource = true)
public PlaybackComponent AddPlaybackComponent(GameObject peerContainer, string roomName, ulong peerId, long mediaStreamId, bool autoDestroySource = true)

Tries to identifiy a gameobject by tag to assign the PlaybackComponent

Parameters

NameTypeDescription
gameObjectTagStringTag string to find with FindGameObjectsWithTag
roomNameStringPlaybackComponent room
peerIdUInt64PlaybackComponent peer
mediaStreamIdInt64PlaybackComponent media
autoDestroySourceBooleanoptionally enable or disable on destroy of PlaybackComponent the destroy of the linked AudioSource
peerContainerGameObjectGameObject to attach to

Returns

TypeDescription
PlaybackComponentScriptReference of from the GameObject or null

Discussion

Creates a PlaybackComponent and attaches it to a GameObject. You can either provide a GameObject tag or you can also provide a GameObject directly. The PlaybackComponent is the audio representation of a peer connected in an ODIN room. Use this function to create a PlaybackComponent connected to the ODIN peer and attach it to the GameObject (i.e. the corresponding player object).

graph LR;
    subgraph Unity Scene
        subgraph Player1 [Player 1]
            PBC1[PlaybackComponent]-->GO1[GameObject]                   
        end
        subgraph Player2 [Player 2]
            PBC2[PlaybackComponent]-->GO2[GameObject]
        end
        
    end
    subgraph ODON Server
        subgraph ODIN Room
            Peer1[Peer 1] --> PBC1
            Peer2[Peer 2] --> PBC2
        end    
    end    
    classDef blue fill:#DDFAFF
    class Player1,Player2 blue

The best place to call this function is in the callback OnCreatedMediaObject as you get all relevant info in the callbacks parameters that you need when calling this function. Please see PlaybackComponent for more info on available settings.

Variants

AddPlaybackComponent(gameObjectTag, roomName, peerId, mediaStreamId, autoDestroySource)

public PlaybackComponent AddPlaybackComponent(string gameObjectTag, string roomName, ulong peerId, long mediaStreamId, bool autoDestroySource = true)

Tries to identifiy a gameobject by tag to assign the PlaybackComponent

Parameters

NameTypeDescription
gameObjectTagStringTag string to find with FindGameObjectsWithTag
roomNameStringPlaybackComponent room
peerIdUInt64PlaybackComponent peer
mediaStreamIdInt64PlaybackComponent media
autoDestroySourceBooleanoptionally enable or disable on destroy of PlaybackComponent the destroy of the linked AudioSource

Returns

TypeDescription
PlaybackComponentScriptReference of from the GameObject or null

AddPlaybackComponent(peerContainer, roomName, peerId, mediaStreamId, autoDestroySource)

public PlaybackComponent AddPlaybackComponent(GameObject peerContainer, string roomName, ulong peerId, long mediaStreamId, bool autoDestroySource = true)

Adds a new PlaybackComponent to the specified GameObject

Parameters

NameTypeDescription
peerContainerGameObjectGameObject to attach to
roomNameStringPlaybackComponent room
peerIdUInt64PlaybackComponent peer
mediaStreamIdInt64PlaybackComponent media
autoDestroySourceBooleanoptionally enable or disable on destroy of PlaybackComponent the destroy of the linked AudioSource

Returns

TypeDescription
PlaybackComponentScriptReference of from the GameObject or null

Example

AddPlaybackComponent Example
private void OnCreatedMediaObject(string roomName, ulong peerId, ushort mediaId)
{
    Room room = OdinHandler.Instance.Rooms[roomName];
    if (room == null || room.Self == null || room.Self.Id == peerId) return;

    // Find the players game object in the scene
    var peerContainer = FindPlayerForPeerId(peerId);

    // Add PlaybackComponent to new dummy PeerCube
    PlaybackComponent playback = OdinHandler.Instance.AddPlaybackComponent(peerContainer, room.Config.Name, peerId, mediaId);

    // Some AudioSource test settings
    playback.PlaybackSource.spatialBlend = 1.0f;
    playback.PlaybackSource.rolloffMode = AudioRolloffMode.Linear;
    playback.PlaybackSource.minDistance = 1;
    playback.PlaybackSource.maxDistance = 10;
}