Audio Channel Events

Inherit from EasyManager.cs

using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}

Inject EasyEvents

using EasyCodeForVivox.Events;
using UnityEngine;
using Zenject;

public class VivoxEvents : MonoBehaviour
{
    EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
}

Dynamic Events

Make sure Dynamic Events are enabled

How to Subscribe to Audio Channel Events in EasyCode

EasyManager

Keeping the base methods are not necessary. They are simply Debug.Logs(). Feel free to delete them

// Voice Channel Event Callbacks

protected override void OnAudioChannelConnecting(IChannelSession channelSession)
{
    base.OnAudioChannelConnecting(channelSession);
}

protected override void OnAudioChannelConnected(IChannelSession channelSession)
{
    base.OnAudioChannelConnected(channelSession);
}

protected override void OnAudioChannelDisconnecting(IChannelSession channelSession)
{
    base.OnAudioChannelDisconnecting(channelSession);
}

protected override void OnAudioChannelDisconnected(IChannelSession channelSession)
{
    base.OnAudioChannelDisconnected(channelSession);
}

EasyEvents

public void SubscribeToAudioChannelEvents()
{
    _events.AudioChannelConnecting += OnAudioChannelConnecting;
    _events.AudioChannelConnected += OnAudioChannelConnected;
    _events.AudioChannelDisconnecting += OnAudioChannelDisconnecting;
    _events.AudioChannelDisconnected += OnAudioChannelDisconnected;
}

public void UnsubscribeToAudioChannelEvents()
{
    _events.AudioChannelConnecting -= OnAudioChannelConnecting;
    _events.AudioChannelConnected -= OnAudioChannelConnected;
    _events.AudioChannelDisconnecting -= OnAudioChannelDisconnecting;
    _events.AudioChannelDisconnected -= OnAudioChannelDisconnected;
}


protected virtual void OnAudioChannelConnecting(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Audio Is Connecting In Channel");
}

protected virtual void OnAudioChannelConnected(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Audio Has Connected In Channel");
}

protected virtual void OnAudioChannelDisconnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Audio Is Disconnecting In Channel");
}

protected virtual void OnAudioChannelDisconnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Audio Has Disconnected In Channel");
}

Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each AudioChannel Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=

[AudioChannelEvent(AudioChannelStatus.AudioChannelConnecting)]
private void OnAudioChannelConnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
}

[AudioChannelEvent(AudioChannelStatus.AudioChannelConnected)]
private void OnAudioChannelConnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
}

[AudioChannelEvent(AudioChannelStatus.AudioChannelDisconnecting)]
private void OnAudioChannelDisconnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
}

[AudioChannelEvent(AudioChannelStatus.AudioChannelDisconnected)]
private void OnAudioChannelDisconnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
}

Dynamic Async Events

Make sure the parameter in your method matches the event type. See what parameter is required for each AudioChannel Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=

Remember to use async void or async Task or else the event may run synchronously

More information on the methods called in any async method can be found here. Unity Gaming Services Examples. They are direct copies from Unity's docs. These are just examples and don't mimic real world use cases

[AudioChannelEventAsync(AudioChannelStatus.AudioChannelConnecting)]
private async void OnAudioChannelConnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
    await LoadPlayerData();
}

[AudioChannelEventAsync(AudioChannelStatus.AudioChannelConnected)]
private async void OnAudioChannelConnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
    await GetJoinedLobbies();
}

[AudioChannelEventAsync(AudioChannelStatus.AudioChannelDisconnecting)]
private async void OnAudioChannelDisconnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
    await SavePlayerData();
}

[AudioChannelEventAsync(AudioChannelStatus.AudioChannelDisconnected)]
private async void OnAudioChannelDisconnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
    await RemovePlayerFromLobby();
}

Last updated