Muting a specific player with photon voice

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on Voice.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Muting a specific player with photon voice

NINOCUNI
2021-10-12 01:28:49

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?

Comments

JohnTube
2021-10-13 07:04:52

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;    
    }    

NINOCUNI
2021-10-15 05:31:33

Thank you. I solved this problem~!

Pihicin
2023-07-27 12:00:13

Hello,

We tried the same method, put a debug log to confirm that in here:

public bool ToggleMuteSpeaker(Speaker speaker, bool mute) {

AudioSource audioSource = speaker.GetComponent();

if(audioSource){

Debug.Log("To mute: " + mute);

audioSource.mute = mute;

}

}

We tried that on PlayStation 5, but it does not work. The log has output correctly. Is there any workarounds?

Back to top