Muting a specific player with photon voice

I want each client connected to the voice server to be able to choose who they hear talking or to put it another way I want each player to be able to mute other players by pushing a button next to their player name.

I can't find anything in the documentation on how this is achieved. Is it by manipulating the voicerecorder on the local instantiated network prefabs that contain the voicerecorder and voicespeaker scripts? Is it as simple as setting voicerecorder.transmit to false in the local instantiation of the network player?

Best Answer

  • JohnTube
    JohnTube mod
    Accepted Answer

    Hi @NINOCUNI,

    Thank you for choosing Photon!

    In general to mute we use AudioSource.mute or AudioSource.volume as we rely on Unity's AudioSource component for playback.

    So you can have a method to mute using Speaker component of a player:

    public bool ToggleMuteSpeaker(Speaker speaker, bool mute) {
        AudioSource audioSource = speaker.GetComponent<AudioSource>();
                  audioSource.mute = mute;
    }
    

    If you don't know which Speaker belongs to which player, you can brute force (not recommended):

    public bool ToggleMutePlayerBad(Player player, bool mute, bool inactive = false) {
         Speaker[] speakers = FindObjectsOfType<Speaker>(inactive);
        foreach(Speaker speaker in speakers) {
            if (speaker.Actor == player) {
                ToggleMuteSpeaker(speaker, mute);
            }
        }
    }
    

    If you use PUN integration, this makes it easier, you can mute by player like this:

    public bool ToggleMutePlayer(Player player, bool mute) {
          if (player != null) {
            int actorNr = player.ActorNumber;
            for(int viewId = actorNr * PhotonNetwork.MAX_VIEW_IDS + 1; viewId < (actorNr + 1) * PhotonNetwork.MAX_VIEW_IDS; viewId++) {
              PhotonView photonView = PhotonView.Find(viewId);
              if (photonView /*&& (photonView.OwnerActorNr == actorNr || photonView.ControllerActorNr == actorNr)*/) {
                PhotonVoiceView photonVoiceView = photonView.GetComponent<PhotonVoiceView>();
                if (photonVoiceView && photonVoiceView.IsSpeaker){
                  ToggleMuteSpeaker(photonVoiceView.SpeakerInUse)
                }
              }
            }
          }
          return false;
        }
    

Answers

  • JohnTube
    JohnTube mod
    Accepted Answer

    Hi @NINOCUNI,

    Thank you for choosing Photon!

    In general to mute we use AudioSource.mute or AudioSource.volume as we rely on Unity's AudioSource component for playback.

    So you can have a method to mute using Speaker component of a player:

    public bool ToggleMuteSpeaker(Speaker speaker, bool mute) {
        AudioSource audioSource = speaker.GetComponent<AudioSource>();
                  audioSource.mute = mute;
    }
    

    If you don't know which Speaker belongs to which player, you can brute force (not recommended):

    public bool ToggleMutePlayerBad(Player player, bool mute, bool inactive = false) {
         Speaker[] speakers = FindObjectsOfType<Speaker>(inactive);
        foreach(Speaker speaker in speakers) {
            if (speaker.Actor == player) {
                ToggleMuteSpeaker(speaker, mute);
            }
        }
    }
    

    If you use PUN integration, this makes it easier, you can mute by player like this:

    public bool ToggleMutePlayer(Player player, bool mute) {
          if (player != null) {
            int actorNr = player.ActorNumber;
            for(int viewId = actorNr * PhotonNetwork.MAX_VIEW_IDS + 1; viewId < (actorNr + 1) * PhotonNetwork.MAX_VIEW_IDS; viewId++) {
              PhotonView photonView = PhotonView.Find(viewId);
              if (photonView /*&& (photonView.OwnerActorNr == actorNr || photonView.ControllerActorNr == actorNr)*/) {
                PhotonVoiceView photonVoiceView = photonView.GetComponent<PhotonVoiceView>();
                if (photonVoiceView && photonVoiceView.IsSpeaker){
                  ToggleMuteSpeaker(photonVoiceView.SpeakerInUse)
                }
              }
            }
          }
          return false;
        }
    
  • Thank you. I solved this problem~!