# Audio Device Events

## Inherit from EasyManager.cs

```csharp
using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}
```

## Inject EasyEvents

```csharp
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

<figure><img src="https://3152232848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWHF2U_tfZEUaf_IsL-%2Fuploads%2FgNWd8mgjCB90w9n3fR8A%2FEnableDynamicEvents.png?alt=media&#x26;token=f9e1f6a3-8d21-49a1-9ff5-d1a8151efad1" alt=""><figcaption></figcaption></figure>

## How to Subscribe to Audio Device Events in EasyCode&#x20;

#### EasyManager

{% hint style="info" %}
Keeping the *<mark style="color:blue;">**base**</mark>* methods are not necessary. They are simply <mark style="color:blue;">**Debug.Logs()**</mark>. Feel free to delete them
{% endhint %}

```csharp
// 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

```csharp
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](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#user-events). 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 <mark style="color:red;">**+=/-=**</mark>

```csharp
[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](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#user-events).  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 <mark style="color:red;">**+=/-=**</mark>

{% hint style="info" %}
**Remember to use&#x20;**<mark style="color:blue;">**async void**</mark>**&#x20;or&#x20;**<mark style="color:blue;">**async Task**</mark>**&#x20;or else the event may run synchronously**
{% endhint %}

{% hint style="warning" %}
**More information on the methods called in any async method can be found here.** [**Unity Gaming Services Examples**](https://fullstackindie.gitbook.io/easy-code-for-vivox/related-info/unity-gaming-services)**. They are direct copies from Unity's docs.&#x20;**<mark style="color:red;">**These are just examples and don't mimic real world use cases**</mark>
{% endhint %}

```csharp
[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();
}
```
