Search code examples
unity-game-enginemultiplayerphoton

How to use Audio Groups in Photon Voice Unity3D?


Hi I am new to multiplayer development and I am using photon voice and wanted to make private voice chat between two player in a room created which has many players. I was directed to https://doc.photonengine.com/en-us/voice/current/getting-started/voice-for-pun?utm_campaign=sendgrid&utm_source=sendgrid.com&utm_medium=email by support of photon but I am not able to get it working. How should I make private voice chat in this multiplayer. please give example for explanation. Thanks


Solution

  • There is a demo scene for Push To Talk which showcases how to do this. Let me try to explain how to implement player to player voice chat using current Photon Voice:

    Photon Voice uses voice groups (which is nothing but Photon LoadBalancing's "Interest Groups") to separate voice channels/targets.

    Filter incoming sounds (select "what to hear" or "who do you want to listen to"):

    Each actor needs to subscribe to voice groups it's interested in. By default all actors listen to audio group 0 which could be seen as a global audio group for voice broadcast. If you want to listen to voice sent to other groups you need to subscribe to them. You can also unsubscribe from previously subscribed ones. The operation to do all this is: PhotonVoiceNetwork.Client.ChangeAudioGroups(byte[] groupsToRemove, byte[] groupsToAdd);

    Select a single transmission target audio group (select "who do you want to talk to"):

    Each actor needs to decide to which voice group it wants to transmit audio. The target audio group can be set using PhotonVoiceRecorder.AudioGroup.


    So depending on the use case what you can do is:

    1. Speak to a single group and listen to multiple groups. You can speak to a group other than those you listen to. You can listen to all available groups.
    2. Speak to a single group and listen only to default group.
    3. Speak and listen to a single same audio group. For this particular use case, there is a shortcut to switch between this single in/out group by setting: PhotonVoiceNetwork.Client.GlobalAudioGroup. If you choose to set GlobalAudioGroup no need to call ChangeAudioGroups or set PhotonVoiceRecorder.AudioGroup as it's done internally for you.

    In the three cases, you always listen to default audio group 0.

    The Photon Voice demo offers two options for private (1 to 1) voice chat:

    • "MuteOthersWhileTalking" enabled: corresponds to case n°3.
    • "MuteOthersWhileTalking" disabled: corresponds to case n°1.

    The audio groups in the demo are constructed this way: We have rooms of 4 actors. We need 6 audio groups. For each pair of actors we calculate a unique group code.

    • actor A with actor number (player ID) equal to x
    • actor B with actor number equal to y

    Here is how we get the audio group of private voice chat between A and B (if an actor number reaches 24 we have a problem):

    if (x < y)
    {
         AudioGroup = y + x * 10;
    }
    else if (x > y)
    {
         AudioGroup = x + y * 10;
    } 
    else 
    {
         // error
    } 
    

    Example: The audio group for actors 1 and 2 is 12.

    Another approach of "calculating" private voice groups is to use the actor number as audio group: each actor subscribes to a single audio group with a code equal to its actor number. whenever you want to talk to a remote actor you set the target audio group (using PhotonVoiceRecorder.AudioGroup only) to the target actor number. The advantage of this approach:

    • Less audio groups: We need as many audio groups as actors.
    • Less audio groups switching: single audio group to subscribe to and no unsubscribing.

    The drawback of this approach:

    • You can't mute any other actor. You will listen to anyone who wants to talk to you privately.