Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Checking for if microphone permission is enabled in android.

Discussion in 'Android' started by darkgriffin, Nov 1, 2018.

  1. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    I have an android game that can optionally listen to audio around the user, and react to it. I am using Microphone.Start() and the built in audio object to get the data and process it.

    It works so long as the user allows permission to the microphone when the application starts. But if they deny the permission, the game continues and then crashes/hangs in my while loop in my code.

    I tried wrapping the entire thing in a check of Application.HasUserAuthorization(UserAuthorization.Microphone) But that doesn't seem to stop the crash, the application still crashes when the scene is entered despite the call being wrapped in the IF statement.

    I can't test properly because in the Unity Editor you always have microphone permission, so I am left puzzled why the android build gets stuck.

    Here is the relevant code I am using, some irrelevant stuff has been removed to simplify the code flow. StartListening() is called by a button press on the enable recording button, it doesn't run at the start. As you can see, the audio while loops should not be able to run if the user failed to give permission, but they clearly do somehow cause the game gets stuck without mic permissions enabled on my test device.

    Code (CSharp):
    1. // Use this for initialization
    2.     void Start () {
    3.         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
    4.             aud.clip = Microphone.Start (null, true, 10, 44100);
    5.             aud.loop = true;
    6.             aud.mute = false;
    7.             //wait for input
    8.             while (!(Microphone.GetPosition (null) > 0)) {
    9.             }
    10.             aud.Play ();
    11.         }
    12.     }
    13.  
    14.     public void StartListening() {
    15.         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
    16.             aud.clip = Microphone.Start (null, true, 10, 44100);
    17.             aud.loop = true;
    18.             aud.mute = false;
    19.             //wait for input
    20.             while (!(Microphone.GetPosition (null) > 0)) {
    21.             }
    22.             aud.Play ();
    23.         } else {
    24.             StartCoroutine ("Start_MicPermissionCheck");
    25.         }
    26.     }
    27.  
    28.     IEnumerator Start_MicPermissionCheck() {
    29.         //ask permission again
    30.         yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
    31.         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
    32.             aud.clip = Microphone.Start (null, true, 10, 44100);
    33.             aud.loop = true;
    34.             aud.mute = false;
    35.             //wait for input
    36.             while (!(Microphone.GetPosition (null) > 0)) {
    37.             }
    38.             aud.Play ();
    39.         } else {
    40.             //permission denyed, just ignore turning it on.
    41.         }
    42.     }
    Most of this code structure is straight from the documentation for UserAuthorization, so I don't know what I'm doing wrong. The game works fine in editor due to having permissions, but on android it gets stuck running the while loop. (which happens if there is no mic permissions and is what I am trying to avoid crashing in the first place). Which indicates to me that this check is always returning TRUE despite the documentation saying otherwise.
     
  2. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    Is this plugin the accepted way of using Android permissions?

    https://github.com/Over17/UnityAndroidPermissions

    It says on that readme that my game cannot be featured by google play if the default Unity permissions are used. (Requesting at the start instead of when the application needs to use the feature).

    Is this still true? If so, what do I need to do to fix this? How can I make the above checks functional on Android?

    My game release is being delayed due to this single issue, I can't afford to flop around for weeks on my own to solve this. Would appreciate a solution from either Unity or the developers on here.

    I simply am trying to create a button that toggles the application listening to the microphone input on and off. But I am trying also not to break any rules for google play store. I would greatly appreciate someone explaining to me how this is supposed to be done, as I have never dealt with the permissions before.
     
  3. mgleleux

    mgleleux

    Joined:
    Apr 19, 2019
    Posts:
    1
    Code (CSharp):
    1. const string micPermissionName = "android.permission.RECORD_AUDIO";
    2.         AndroidPermissionsManager.RequestPermission(micPermissionName)
    3.             .ThenAction((grantResult) =>
    4.             {
    5.                 _selectedDevice = Microphone.devices[0].ToString();
    6.                 _audioSource.outputAudioMixerGroup = _mixerGroupMicrophone;
    7.                 _audioSource.clip = Microphone.Start(_selectedDevice, true, 10, AudioSettings.outputSampleRate);
    8.                 while (!(Microphone.GetPosition(null) > 0)) { }
    9.                 _audioSource.Play();
    10.             });
    Just in case this issue hasn't been solved by anyone. Finally figured this one out and figured id share.
     
    immersification likes this.
  4. lsiguis

    lsiguis

    Joined:
    Jul 14, 2019
    Posts:
    2
    how do I enable microphone in unity editor for android sdk? thankee
     
  5. Realform

    Realform

    Joined:
    Mar 20, 2015
    Posts:
    9
    just complementing this is ok but it needs dependencies from AndroidPermissionsManager which is also something used for AR aplications.
    https://github.com/google-ar/arcore...Core/SDK/Scripts/AndroidPermissionsManager.cs