Microphone usage priority / Google Assistant / Navigator app (UI/Background)

299 Views Asked by At

So there are some differences how the Android works with microphone for multiple apps:

Android 9 https://developer.android.com/guide/topics/media/sharing-audio-input#pre-behavior and https://source.android.com/docs/core/audio/concurrent#assistants-not-blocking-each-other

Android 10 https://developer.android.com/guide/topics/media/sharing-audio-input#behavior

Android 11 https://developer.android.com/guide/topics/media/sharing-audio-input#concurrent-capture-r

Some users of my video recorder app (which can record videos in background by using foreground service) were leaving negative reviews that my app prioritizes the microphone and prevents it to be used for other apps. So basically they try to use both my app and some other at the same time (e.g., some maps navigator, assistant and so on). So I had this code before:

// MediaRecorder context
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)

And then I replaced it with the following code to try to fix the mentioned issue:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Android 11+
    setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
    isPrivacySensitive = false
} else { // use MIC instead of CAMCORDER to prevent app prioritizing mic for itself (at least for Android 10)
    setAudioSource(MediaRecorder.AudioSource.MIC)
}

But now I receive some negative reviews from users on Android 9 up to Android 12/13 that my app doesn't record audio. It's just a dead end...

I am concerned about Google (or other) Assistant app which always uses microphone for hotword.

So what would be the best configuration for Android 9-, Android 10 and Android 11+ based on this?

For Android 10+ it should be possible for apps to use mic concurrently based on the doc, Assistant app should not take the stream to itself until it has visible UI as I understood so both Assistant app in background mode and my app in any mode should NOT receive silence.

Mb only for Android 9- I need to return priority to my app and keep everything else as it setAudioSource(MediaRecorder.AudioSource.CAMCORDER), because there is no solution and mic can be used only with one app at the same time:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Android 11+
    setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
    isPrivacySensitive = false
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { // Android 10
    setAudioSource(MediaRecorder.AudioSource.MIC)
} else { // Android 9-
    setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
}

Or for Android 9- mb it doesn't matter if it's MIC or CAMCORDER, I guess based on the doc the first app that launches takes the priority and that's it. But after update I received a review from a user that says that my app on his Android 9 device (Xiaomi Redmi Note 5 Pro) doesn't record audio, so it seems it still effects some devices... Also I don't understand how it works with Assistant apps which are already started on device boot.

1

There are 1 best solutions below

0
user924 On

I guess there is no normal solution.

Decided to add an option for users to switch audio priority for this app so they manually could control it:

if (prefsManager.isAudioPriority || Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    // if a user set audio priority for this app or Android 9-
    setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
} else {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Android 11+
        setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
        isPrivacySensitive = false
    } else { // Android 10
        setAudioSource(MediaRecorder.AudioSource.MIC)
    }
}