[SOLVED] MicPermission in Android build (Quest) - Voice not working

Am0theKing
edited January 2021 in Photon Voice
Hi folks,
I have a problem with the VR app I currently work on - so I have implemented the Photon Voice and in the Unity Editor it works just fine but when I do a build for the Quest I always need to restart the application after the first install.
The issue is linked to the MicPermissions which are need in the android build - I found this post here and it is describing basically the same issue I have here.

So I have a gameobject with the "Recorder" component and the "Photon Voice Network" component attached and its already in the scene on start.

I have a script that checks for the permissions on Start of the application:
using UnityEngine;
using Photon.Voice.Unity;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.Android;


public class NetworkVoiceController : MonoBehaviourPunCallbacks
{

    Recorder recorder;

    public bool hasMicPermission;
    bool isRequesting;

    bool hasRestartedRecorder = false;

    // Start is called before the first frame update
    void Start()
    {
        recorder = GetComponent<Recorder>();
        //set the microphone
        recorder.UnityMicrophoneDevice = Microphone.devices[0];

        InitVoiceController();
    }

    public void InitVoiceController()
    {
        if(Permission.HasUserAuthorizedPermission(Permission.Microphone))
        {
            hasMicPermission = true;
            recorder.RestartRecording();
            hasRestartedRecorder = true;
        }
        else
        {
            Permission.RequestUserPermission(Permission.Microphone);
            isRequesting = true;
        }
    }

    private void OnApplicationFocus(bool focus)
    {
        if (focus && isRequesting)
        {
            if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
            {
                hasMicPermission = true;
                recorder.RestartRecording();
                hasRestartedRecorder = true;
            }
            else
            {
                hasMicPermission = false;
            }
            isRequesting = false;
        }
    }

    public void StartRecording()
    {
        if (hasMicPermission)
        {
            recorder.TransmitEnabled = true;
            recorder.StartRecording();
        }
    }

    public void StopRecording()
    {
        if (!hasMicPermission)
        {
            recorder.StopRecording();
            recorder.TransmitEnabled = false;
        }
    }

    private void Update()
    {
        if(recorder == null)
        {
            recorder = GetComponent<Recorder>();
        }

        if (recorder.UnityMicrophoneDevice != Microphone.devices[0])
        {
            recorder.UnityMicrophoneDevice = Microphone.devices[0];
        }

        if(hasMicPermission && recorder.IsRecording == false)
        {
            StartRecording();
        }

        if (!PlayerSettings.Instance.useVoice)
        {
            GetRecorder().TransmitEnabled = false;
        }
    }

Do I need to instantiate the Recorder/VoiceNetwork after I have checked for the permissions or do I need to restart them somehow?

I am a bit unsure how to solve this - so a little guidance would help me quite a lot.
Thanks in advance

Comments

  • So I think I found the cause of this issue - its me setting the microphone device manually. So I removed that and now it works like a charm.

    Final Code:
    using UnityEngine;
    using Photon.Voice.Unity;
    using Photon.Pun;
    using Photon.Realtime;
    using UnityEngine.Android;
    
    
    public class NetworkVoiceController : MonoBehaviourPunCallbacks
    {
    
        Recorder recorder;
    
        public bool hasMicPermission;
        bool isRequesting;
    
        bool hasRestartedRecorder = false;
    
        // Start is called before the first frame update
        void Start()
        {
            recorder = GetComponent<Recorder>();
         
            InitVoiceController();
        }
    
        public void InitVoiceController()
        {
            if(Permission.HasUserAuthorizedPermission(Permission.Microphone))
            {
                hasMicPermission = true;
                recorder.RestartRecording();
                hasRestartedRecorder = true;
            }
            else
            {
                Permission.RequestUserPermission(Permission.Microphone);
                isRequesting = true;
            }
        }
    
        private void OnApplicationFocus(bool focus)
        {
            if (focus && isRequesting)
            {
                if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
                {
                    hasMicPermission = true;
                    recorder.RestartRecording();
                    hasRestartedRecorder = true;
                }
                else
                {
                    hasMicPermission = false;
                }
                isRequesting = false;
            }
        }
    
        public void StartRecording()
        {
            if (hasMicPermission)
            {
                recorder.TransmitEnabled = true;
                recorder.StartRecording();
            }
        }
    
        public void StopRecording()
        {
            if (!hasMicPermission)
            {
                recorder.StopRecording();
                recorder.TransmitEnabled = false;
            }
        }
    
        private void Update()
        {
            if(recorder == null)
            {
                recorder = GetComponent<Recorder>();
            }
    
            if(hasMicPermission && recorder.IsRecording == false)
            {
                StartRecording();
            }
    
            if (!PlayerSettings.Instance.useVoice)
            {
                GetRecorder().TransmitEnabled = false;
            }
        }