# Mute / Unmute 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 Mute/Unmute 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
// User Audio Event Callbacks

protected override void OnUserMuted(IParticipant participant)
{
    base.OnUserMuted(participant);
}

protected override void OnUserUnmuted(IParticipant participant)
{
    base.OnUserUnmuted(participant);
}

protected override void OnUserNotSpeaking(IParticipant participant)
{
    base.OnUserNotSpeaking(participant);
}

protected override void OnUserSpeaking(IParticipant participant)
{
    base.OnUserSpeaking(participant);
}

protected override void OnLocalUserMuted()
{
    base.OnLocalUserMuted();
}

protected override void OnLocalUserUnmuted()
{
    base.OnLocalUserUnmuted();
}

protected override void OnCrossMuted(AccountId accountId)
{
    base.OnCrossMuted(accountId);
}

protected override void OnCrossUnmuted(AccountId accountId)
{
    base.OnCrossUnmuted(accountId);
}
```

#### EasyEvents

```csharp
public void SubscribeToMuteEvents()
{
    _events.UserMuted += OnUserMuted;
    _events.UserUnmuted += OnUserUnmuted;
    _events.UserSpeaking += OnUserSpeaking;
    _events.UserNotSpeaking += OnUserNotSpeaking;
    _events.LocalUserMuted += OnLocalUserMuted;
    _events.LocalUserUnmuted += OnLocalUserUnmuted;
    _events.UserCrossMuted += OnCrossMuted;
    _events.UserCrossUnmuted += OnCrossUnmuted;
}

public void UnsubscribeToMuteEvents()
{
    _events.UserMuted -= OnUserMuted;
    _events.UserUnmuted -= OnUserUnmuted;
    _events.UserSpeaking -= OnUserSpeaking;
    _events.UserNotSpeaking -= OnUserNotSpeaking;
    _events.LocalUserMuted -= OnLocalUserMuted;
    _events.LocalUserUnmuted -= OnLocalUserUnmuted;
    _events.UserCrossMuted -= OnCrossMuted;
    _events.UserCrossUnmuted -= OnCrossUnmuted;
}



protected virtual void OnLocalUserMuted()
{
    Debug.Log("Local User is Muted");
}

protected virtual void OnLocalUserUnmuted()
{
    Debug.Log("Local User is Unmuted");
}

protected virtual void OnCrossMuted(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Muted");
}

protected virtual void OnCrossUnmuted(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Unmuted");
}

protected virtual void OnUserMuted(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Muted : (Muted For All : {participant.IsMutedForAll})");
}

protected virtual void OnUserUnmuted(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Unmuted : (Muted For All : {participant.IsMutedForAll})");
}

protected virtual void OnUserSpeaking(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Speaking : Audio Energy {participant.AudioEnergy}");
}

protected virtual void OnUserNotSpeaking(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Not Speaking");
}
```

#### 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
[UserEvent(UserStatus.LocalUserMuted)]
private void OnLocalUserMuted()
{
    Debug.Log("Local User is Muted");
}

[UserEvent(UserStatus.LocalUserUnmuted)]
private void OnLocalUserUnmuted()
{
    Debug.Log("Local User is Unmuted");
}

[UserEvent(UserStatus.UserCrossMuted)]
private void OnCrossMuted(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Muted");
}

[UserEvent(UserStatus.UserCrossUnmuted)]
private void OnCrossUnmuted(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Unmuted");
}

[UserEvent(UserStatus.UserMuted)]
private void OnUserMuted(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Muted : (Muted For All : {participant.IsMutedForAll})");
}

[UserEvent(UserStatus.UserUnmuted)]
private void OnUserUnmuted(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Unmuted : (Muted For All : {participant.IsMutedForAll})");
}

[UserEvent(UserStatus.UserSpeaking)]
private void OnUserSpeaking(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Speaking : Audio Energy {participant.AudioEnergy}");
}

[UserEvent(UserStatus.UserNotSpeaking)]
private void OnUserNotSpeaking(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Not Speaking");
}
```

#### 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
[UserEventAsync(UserStatus.LocalUserMuted)]
private async void OnLocalUserMutedAsync()
{
    Debug.Log("Local User is Muted");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.LocalUserUnmuted)]
private async void OnLocalUserUnmutedAsync()
{
    Debug.Log("Local User is Unmuted");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserCrossMuted)]
private async void OnCrossMutedAsync(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Muted");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserCrossUnmuted)]
private async void OnCrossUnmutedAsync(AccountId accountId)
{
    Debug.Log($"Player {accountId.DisplayName} has been Cross Unmuted");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserMuted)]
private async void OnUserMutedAsync(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Muted : (Muted For All : {participant.IsMutedForAll})");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserUnmuted)]
private async void OnUserUnmutedAsync(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Unmuted : (Muted For All : {participant.IsMutedForAll})");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserSpeaking)]
private async void OnUserSpeakingAsync(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Speaking : Audio Energy {participant.AudioEnergy}");
    await SavePlayerData();
}

[UserEventAsync(UserStatus.UserNotSpeaking)]
private async void OnUserNotSpeakingAsync(IParticipant participant)
{
    Debug.Log($"{participant.Account.DisplayName} Is Not Speaking");
    await SavePlayerData();
}
```
