Audio Device 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 Device Events in EasyCode

EasyManager

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

// Audio Device Events

protected override void OnAudioInputDeviceAdded(IAudioDevice audioDevice)
{
    base.OnAudioInputDeviceAdded(audioDevice);
}

protected override void OnAudioInputDeviceRemoved(IAudioDevice audioDevice)
{
    base.OnAudioInputDeviceRemoved(audioDevice);
}

protected override void OnAudioInputDeviceUpdated(IAudioDevice audioDevice)
{
    base.OnAudioInputDeviceUpdated(audioDevice);
}

protected override void OnAudioOutputDeviceAdded(IAudioDevice audioDevice)
{
    base.OnAudioOutputDeviceAdded(audioDevice);
}

protected override void OnAudioOutputDeviceRemoved(IAudioDevice audioDevice)
{
    base.OnAudioOutputDeviceRemoved(audioDevice);
}

protected override void OnAudioOutputDeviceUpdated(IAudioDevice audioDevice)
{
    base.OnAudioOutputDeviceUpdated(audioDevice);
}

EasyEvents

public void SubscribeToAudioDeviceEvents()
{
    _events.AudioInputDeviceAdded += OnAudioInputDeviceAdded;
    _events.AudioInputDeviceRemoved += OnAudioInputDeviceRemoved;
    _events.AudioInputDeviceUpdated += OnAudioInputDeviceUpdated;

    _events.AudioOutputDeviceAdded += OnAudioOutputDeviceAdded;
    _events.AudioOutputDeviceRemoved += OnAudioOutputDeviceRemoved;
    _events.AudioOutputDeviceUpdated += OnAudioOutputDeviceUpdated;
}

public void UnsubscribeFromAudioDeviceEvents()
{
    _events.AudioInputDeviceAdded -= OnAudioInputDeviceAdded;
    _events.AudioInputDeviceRemoved -= OnAudioInputDeviceRemoved;
    _events.AudioInputDeviceUpdated -= OnAudioInputDeviceUpdated;

    _events.AudioOutputDeviceAdded -= OnAudioOutputDeviceAdded;
    _events.AudioOutputDeviceRemoved -= OnAudioOutputDeviceRemoved;
    _events.AudioOutputDeviceUpdated -= OnAudioOutputDeviceUpdated;
}



protected virtual void OnAudioInputDeviceAdded(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been added {audioDevice?.Name}");
}

protected virtual void OnAudioInputDeviceRemoved(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been removed {audioDevice?.Name}");
}

protected virtual void OnAudioInputDeviceUpdated(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input Device has been changed to {audioDevice?.Name}");
}

protected virtual void OnAudioOutputDeviceAdded(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been added {audioDevice?.Name}");
}

protected virtual void OnAudioOutputDeviceRemoved(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been removed {audioDevice?.Name}");
}

protected virtual void OnAudioOutputDeviceUpdated(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output Device has been changed to {audioDevice?.Name}");
}

Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each User 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 +=/-=

[AudioDeviceEvent(AudioDeviceStatus.AudioInputDeviceAdded)]
private void OnAudioInputDeviceAdded(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been added {audioDevice?.Name}");
}

[AudioDeviceEvent(AudioDeviceStatus.AudioInputDeviceRemoved)]
private void OnAudioInputDeviceRemoved(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been removed {audioDevice?.Name}");
}

[AudioDeviceEvent(AudioDeviceStatus.AudioInputDeviceUpdated)]
private void OnAudioInputDeviceUpdated(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input Device has been changed to {audioDevice?.Name}");
}

[AudioDeviceEvent(AudioDeviceStatus.AudioOutputDeviceAdded)]
private void OnAudioOutputDeviceAdded(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been added {audioDevice?.Name}");
}

[AudioDeviceEvent(AudioDeviceStatus.AudioOutputDeviceRemoved)]
private void OnAudioOutputDeviceRemoved(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been removed {audioDevice?.Name}");
}

[AudioDeviceEvent(AudioDeviceStatus.AudioOutputDeviceUpdated)]
private void OnAudioOutputDeviceUpdated(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output Device has been changed to {audioDevice?.Name}");
}

Dynamic Async Events

Make sure the parameter in your method matches the event type. See what parameter is required for each User 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

[AudioDeviceEventAsync(AudioDeviceStatus.AudioInputDeviceAdded)]
private async void OnAudioInputDeviceAddedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been added {audioDevice?.Name}");
    await SavePlayerData();
}

[AudioDeviceEventAsync(AudioDeviceStatus.AudioInputDeviceRemoved)]
private async void OnAudioInputDeviceRemovedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input device has been removed {audioDevice?.Name}");
    await SavePlayerData();
}

[AudioDeviceEventAsync(AudioDeviceStatus.AudioInputDeviceUpdated)]
private async void OnAudioInputDeviceUpdatedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Input Device has been changed to {audioDevice?.Name}");
    await SavePlayerData();
}

[AudioDeviceEventAsync(AudioDeviceStatus.AudioOutputDeviceAdded)]
private async void OnAudioOutputDeviceAddedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been added {audioDevice?.Name}");
    await SavePlayerData();
}

[AudioDeviceEventAsync(AudioDeviceStatus.AudioOutputDeviceRemoved)]
private async void OnAudioOutputDeviceRemovedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output device has been removed {audioDevice?.Name}");
    await SavePlayerData();
}

[AudioDeviceEventAsync(AudioDeviceStatus.AudioOutputDeviceUpdated)]
private async void OnAudioOutputDeviceUpdatedAsync(IAudioDevice audioDevice)
{
    Debug.Log($"Audio Output Device has been changed to {audioDevice?.Name}");
    await SavePlayerData();
}

Last updated