Voice - PUN2 Integration

Contents

Voice - PUN2 Integration

The main use is that each player has a network-instantiated PUN prefab (character or avatar) where audio is 3D positioned. Audio streams are created in the Voice room synchronously with PUN prefab instantiation in the scene. The Recorder attached to the network object produces an audio stream. The Speaker attached to the remote copy of this network object consumes the audio stream. Photon View ID is used to find the correct Speaker for the Recorder.

With this integration it's also possible to simply make the Voice room follow the state of the PUN room and stream through PunVoiceClient.Instance.PrimaryRecorder not bound to any network object.

Back To Top

Key Components

PunVoiceClient

An implementation of VoiceConnection that maintains connections and synchronizes the states of 2 rooms: Voice and PUN.

PunVoiceClient is a singleton: one instance only should exist. If multiple instances are present, only one will be kept and others will be destroyed. If you add PunVoiceClient from a prefab, make sure it's instantiated once.

Back To Top

Application Settings

  • PunVoiceClient.Instance.UsePunAppSettings: if true, AppSettings from PUN's PhotonNetwork.PhotonServerSettings are used. Otherwise, set the values specific to the PunVoiceClient singleton in the "App Settings" foldout.

  • PunVoiceClient.Instance.UsePunAuthValues: if true, the Voice client will use the same AuthenticationValues as PUN's PhotonNetwork.AuthValues. This requires the Voice application to have the same custom authentication dashboard configuration as the PUN application.

Back To Top

Following The PUN State

  • PunVoiceClient.Instance.AutoConnectAndJoin: if PUN client joins a room, the Voice client connects and joins the corresponding Voice room as well.

  • PunVoiceClient.Instance.AutoLeaveAndDisconnect: if PUN client leaves the room, the Voice client disconnects automatically.

Back To Top

Streaming

  • PunVoiceClient.Instance.PrimaryRecorder: an object with a Recorder component useful for scenarios where there is only 1 outgoing stream per client.

  • PunVoiceClient.Instance.UsePrimaryRecorder: if true, the Recorder attached to the Primary Recorder object will be automatically initialized and used for streaming. The Recorder will not be bound to any network object. Uncheck it if you are using PhotonVoiceView prefabs.

  • PunVoiceClient.Instance.SpeakerPrefab: a prefab that will be used to instantiate a speaker object when a new incoming stream is detected and the object that this stream belongs to does not have a Speaker in its hierarchy. Set it if you are not using PhotonVoiceView prefabs or these prefabs do not have a Speaker component. Must contain a Speaker component.

Back To Top

PhotonVoiceView

The component assigned to the PhotoView prefab and responsible for setting up the Recorder and Speaker. On the streaming side, it starts the Recorder found in the prefab hierarchy, or if not found, PunVoiceClient.Instance.PrimaryRecorder. On the receiving side, It starts the Speaker found in the prefab hierarchy, or if not found, instantiates it from PunVoiceClient.Instance.SpeakerPrefab.

Back To Top

Suggested Workflow

Scene Setup

  1. Set Voice AppId in the AppSettings from PUN's PhotonServerSettings.
  2. Add a PunVoiceClient to the scene.
  3. Add an object with a Recorder to the scene and assign it to PunVoiceClient.PrimaryRecorder.
  4. Create a prefab with a Speaker and assign it to PunVoiceClient.SpeakerPrefab.

Back To Top

Prefab Setup

  1. Add a PhotonVoiceView to the PUN prefab object that has a PhotonView attached to it.
  2. Instantiate this prefab after joining a room.

See see sample for PUN2 for more information .

To Document Top