Microphone Handling - What's is the correct way to do it?

We use Microphone in our app but we have some serious issues with it on iOS:

1) By default when activating a Microphone on iOS, the app freezes for a few noticeable moments;
2) We can fix #1 by checking “Prepare iOS for Recording” on PlayerSettings;
3) By doing #2, the default speaker is now the earphone speaker, which is undesirable. To fix this, we check “Force iOS Speakers when Recording”;
4) Due to #3, now Bluetooth phones and AirPlay stop working, which we can fix by calling the following native code when Mic is activated/deactivated:

[[AVAudioSession sharedInstance]
    setCategory:AVAudioSessionCategoryPlayAndRecord
    withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowAirPlay
error:nil];

5) This solution brings us back to problem #1, which are the freezes on activating Microphone…

What is the proper way to handle this?
The requirements are:

  • Don’t freeze app when microphone activates;
  • Use built-mic (not Bluetooth)
  • Support Bluetooth phones and AirPlay

I understand that it’s possible to pre-activate the microphone, but that would not be possible if we wished to ask permission just before the microphone is used for the first time.

Why is there nothing on Unity like Microphone.PrepareForRecording()?
What configurations exactly do PlayerSettings’ “Prepare iOS for Recording” and “Force iOS Speakers when Recording” set?

I have searched on many topics (not only on this forum) but couldn’t find a conclusive way to fix this.

1 Like

So, it seems that no one has an answer to this question or care about answering it.

What we did to “fix” it: on iOS, we enable the mic as soon as the scene that might use it is loaded combined with alternative #4. That prevents the app from freezing when enabling the mic. It’s not the perfect solution, but it’s what we have today.

It would be nice to simply have a “PrepareForRecording” method on Unity though… :smile:

Running into the same issue here. What did you do to enable the mic as soon as the scene loaded?

We enable the mic as soon as the scene initializes and call that native iOS method above right after calling “Microphone.Start”. :frowning:

Interesting. So you force enable the mic on scene start - for how long? I suppose you did that to get the first “freeze” out of the way?

Exactly.
We disable the mic when the user leaves the scene in which the interaction occurs.