Getting a Voice error when setting DisconnectAfterKeepAlive to true

CheeseBrownie
edited April 2023 in Photon Voice

I have set DisconnectAfterKeepAlive  to true in ConnectionHandler.cs and changed KeepAliveInBackground to 5 seconds so players disconnect from the room faster when the app goes into the background. However I'm getting this error which is preventing the player from reconnecting to Voice when they resume. This only happens if DisconnectAfterKeepAlive  is set to True. Current PUN2 version is 2.40. Voice version is 2.50

UnityException: EndRecord can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Microphone.End (System.String deviceName) (at <b4ff5a062d2b4f0f8b2d2cfa2ddd7f34>:0)
Photon.Voice.Unity.UnityMicrophone.End (System.String deviceName) (at Assets/Plugins/Photon/PhotonVoice/PhotonVoiceApi/Platforms/Unity/UnityMicrophone.cs:32)
Photon.Voice.Unity.MicWrapper.Dispose () (at Assets/Plugins/Photon/PhotonVoice/PhotonVoiceApi/Platforms/Unity/MicWrapper.cs:63)
Photon.Voice.Unity.Recorder.RemoveVoice () (at Assets/Plugins/Photon/PhotonVoice/Code/Recorder.cs:1788)
Photon.Voice.Unity.Recorder.StopRecordingInternal () (at Assets/Plugins/Photon/PhotonVoice/Code/Recorder.cs:2329)
Photon.Voice.Unity.VoiceConnection.StopInitializedRecorders () (at Assets/Plugins/Photon/PhotonVoice/Code/VoiceConnection.cs:1024)
Photon.Voice.Unity.VoiceConnection.OnVoiceStateChanged (Photon.Realtime.ClientState fromState, Photon.Realtime.ClientState toState) (at Assets/Plugins/Photon/PhotonVoice/Code/VoiceConnection.cs:812)
Photon.Voice.PUN.PhotonVoiceNetwork.OnVoiceStateChanged (Photon.Realtime.ClientState fromState, Photon.Realtime.ClientState toState) (at Assets/Plugins/Photon/PhotonVoice/Code/PUN/PhotonVoiceNetwork.cs:294)
Photon.Realtime.LoadBalancingClient.set_State (Photon.Realtime.ClientState value) (at Assets/Plugins/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:452)
Photon.Realtime.LoadBalancingClient.Disconnect (Photon.Realtime.DisconnectCause cause) (at Assets/Plugins/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:1280)
Photon.Realtime.ConnectionHandler.RealtimeFallbackThread () (at Assets/Plugins/Photon/PhotonRealtime/Code/ConnectionHandler.cs:180)
ExitGames.Client.Photon.SupportClass+<>c__DisplayClass6_0.<StartBackgroundCalls>b__0 () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/SupportClass.cs:142)
System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Threading.ThreadHelper.ThreadStart () (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.Microphone:End(String)
Photon.Voice.Unity.UnityMicrophone:End(String) (at Assets/Plugins/Photon/PhotonVoice/PhotonVoiceApi/Platforms/Unity/UnityMicrophone.cs:32)
Photon.Voice.Unity.MicWrapper:Dispose() (at Assets/Plugins/Photon/PhotonVoice/PhotonVoiceApi/Platforms/Unity/MicWrapper.cs:63)
Photon.Voice.Unity.Recorder:RemoveVoice() (at Assets/Plugins/Photon/PhotonVoice/Code/Recorder.cs:1788)
Photon.Voice.Unity.Recorder:StopRecordingInternal() (at Assets/Plugins/Photon/PhotonVoice/Code/Recorder.cs:2329)
Photon.Voice.Unity.VoiceConnection:StopInitializedRecorders() (at Assets/Plugins/Photon/PhotonVoice/Code/VoiceConnection.cs:1024)
Photon.Voice.Unity.VoiceConnection:OnVoiceStateChanged(ClientState, ClientState) (at Assets/Plugins/Photon/PhotonVoice/Code/VoiceConnection.cs:812)
Photon.Voice.PUN.PhotonVoiceNetwork:OnVoiceStateChanged(ClientState, ClientState) (at Assets/Plugins/Photon/PhotonVoice/Code/PUN/PhotonVoiceNetwork.cs:294)
Photon.Realtime.LoadBalancingClient:set_State(ClientState) (at Assets/Plugins/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:452)
Photon.Realtime.LoadBalancingClient:Disconnect(DisconnectCause) (at Assets/Plugins/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:1280)

Comments

  • The code in the stack does not look like from Voice v2.50.

    Please make sure that you have updated to the current version. It calls MicWrapper.Dispose() from Update() now.