Currently Photon Voice is available only as a Unity package downloadable directly from the Asset Store here. It is an add-on that brings voice chat and audio streaming to PUN. Photon Voice lets you focus on audio transmission features and can handle the connection workflow for you. While Photon Voice depends on PUN and extends it, it requires a separate AppId of its own that you can get here.
Photon Voice Unity package, also called PUNVoice, includes PUN (minus PUN demos files and Photon Chat API). If you want to start using both packages just download and import Photon Voice alone. If you already use PUN and want to add Voice, it is preferable that you clean up PUN and import Photon Voice only. This way we guarantee compatibility since PUN and PUNVoice packages are updated separately.
Once you import Photon Voice to the project, a "Photon Voice Settings" section should appear to PhotonServerSettings just below PUN settings.
There you can enter the
AppId of your Photon Voice application.
Remember that you can always reopen the PhotonServerSettings in the Unity Inspector by following these steps: "Window" -> "Photon Unity Networking" -> "Highlight Server Settings".
To tweak advanced audio and voice related settings, make sure a PhotonVoiceSettings script is attached to one object in the scene. The available settings are described as follows:
- PUN related settings:
AutoConnect: automatically joins Photon Voice client to a "voice room" when PUN client is joined to a "PUN room".
AutoDisconnect: automatically disconnects Photon Voice client when PUN client is disconnected.
AutoTransmit: starts transmitting audio as soon as Photon Voice client is joined to a "voice room".
- Microphone and "local" audio recording settings that will be applied to every "recorder" instance created:
SamplingRate: a frequency of how many times audio is measured per second. Generally, this defines the audio quality you want. Possible values are: 8, 12, 16, 24 and 48 kHz. Default is 24 kHz.
Delay: outgoing audio stream encoder delay in milliseconds (buffer size in terms of time). Possible values are 5, 10, 20, 40, 80 and 120 ms. Default is 20 ms.
Bitrate: the amount of data (the number of bits) that are processed over a certain amount of time (second). Generally, this defines the compression quality. Default value is 30000 b/s.
- "Remote" audio streaming settings that will be applied to every "speaker" instance created:
PlayDelayMs: playback delay in milliseconds. Used to compensate incoming packets latency variations.
- Voice detection feature (will be applied to every "recorder" instance created):
VoiceDetection: toggles voice detection feature.
VoiceDetectionThreshold: minimal signal level to be exceeded to start transmission if voice detection is enabled. 0.01 is the default and recommended value.
DebugInfo: toggles Photon Voice debug logs in Unity console at info level.
DebugLostPercent: lost frames simulation ratio.
The "Audio Source" Prefab
Photon Voice for PUN expects each "audio source" object to be represented by a prefab.
The minimal required prefab should contain two script components attached to the same GameObject:
AudioSourceshould be used exclusively for voice chat by Photon Voice. If you want to have other sounds per player, use a separate
AudioSourceattached to a separate
After adding the PhotonVoiceRecorder script, you will notice that an AudioSource component has been added and also a PhotonVoiceSpeaker script. Both are essential to reproducing received remote audio from other clients joined to the same room.
The prefab should be instantiated only at runtime and after joining a room using PUN. Scene object instances are not supported. So you should not have any PhotonVoiceRecorder instances in the scene.
The audio source prefab is used for two different purposes:
- Record and transmit "local" sound:
This is handled by PhotonVoiceRecorder. Check the
IsTransmittingproperty of the same component to know if the recorded audio is being sent or not.
- Reproduce and play received "remote" sound:
This is managed by PhotonVoiceSpeaker. Use its
IsPlayingproperty to see if a remote received audio is being played.
This audio source prefab can be used in two different ways:
- Voice Chat:
If no audio clip is assigned to PhotonVoiceRecorder and if a microphone device is detected, audio will be recorded and transmitted to all other joined players. In case multiple recording devices are available, the one to be used need to be set to
- Streaming Audio Clips:
This option was introduced to test Photon Voice transmission. However, you may use it for other purposes. In order to broadcast an audio clip to other players joined to the same room, you need to assign an audio media file supported by Unity to
PhotonVoiceRecorder.AudioClip. This can be done by a drag and drop to the Inspector from Unity Editor. If you want the audio clip to be replayed every time make sure
PhotonVoiceRecorder.Loopis set to
Voice detection is an optional feature that will filter recorded sound and transmits only when a predefined threshold of signal level is exceeded. This means that voice transmission is automatically paused when you stop speaking and it is resumed when you start talking. This will also help you avoid sending useless noise and thus reduce traffic.
The default value of
VoiceDetectionThreshold is 0,01.
It is the recommended value for common environments as a result of experiments with voice detection calibration and noise level measurements.
If you still experience issues when recording your voice even with voice detection is on, you may need voice calibration.
Photon Voice offers an auto calibration mode which is limited in time.
To start calibration mode you should call
The default calibration period is 2000 milliseconds but you can always change it.
Using calibration, Photon Voice automatically adjusts silence and loudness thresholds.
It is easy to have a push-to-talk feature with Photon Voice.
You need to manually toggle voice recording and transmission.
It's like turning microphone mute on and off.
To start push-to-talk just set
To stop it just set the value back to
You can achieve this by binding the property to a user input (UI button or keyboard shortcut).
Photon Voice is not for broadcast only.
You can offer your players the possibility to have multiple conversations going on at the same time without interfering with each other.
This can be done using "Audio Groups" which makes use of Photon's "Interest Groups" feature.
An "Audio Group" is identified by a number (
You can subscribe and unsubscribe from multiple groups.
However, you can only transmit audio to one group at a time.
By default, all voice clients are subscribed to the group "0": each player can hear everyone else and talk to everyone else.
If you want to replace this default behaviour, change the "Global Audio Group" using the property with the same name
Only players registered to that group will hear the audio you transmit.
On the other hand, you can select more than one group you want to listen to. To update the list of groups you're registered to, you can use the following method:
PhotonVoiceNetwork.Client.ChangeAudioGroups(byte groupsToRemove, byte groupsToAdd);
You should take into consideration the following notes when changing audio groups:
- It can be done only when joined to a "voice room".
- If you don't want to add or remove any group pass a
nullin the respective parameter.
- If you want to add or remove all existing groups pass an empty array (
byte) in the respective parameter.
- You cannot unsubscribe from default audio group (0).
Audio groups can be combined with Push-to-Talk to obtain nice features for your game.
Example Use Case 1: Private Conversations
You can add private voice chat in few steps:
- If n is the number of players create n * (n - 1) audio group numbers and bind each one to a couple of players. Example: Player 1 and Player 2 can subscribe to group "12" or "21".
- A player should subscribe to all audio groups shared with another player at once or subscribe on demand for a "phone call like" feature.
- A player can talk to exactly one player by setting the
GlobalAudioGroupto the corresponding shared "binary audio group" number.
Example Use Case 2: Team Chat
If you are implementing teams in your game, you can add "team chat" easily as follows:
- Assign a group number per team.
- A player should subscribe to the audio group of the team he/she belongs to.
- Players of the same team can talk to each other by setting the
GlobalAudioGroupto the team's audio group number.