On one of my apps activities a user can start an AudioSession
. This AudioSession
runs until the user explicitly stops it in the app, or the app is terminated. During an AudioSession
, AudioRecord and AudioTrack are used to record and play audio. Recording and playing should continue even if my app is minimized or the screen goes off.
As far as I understand I want to record audio in background. There are several restrictions on this in different Android versions (correct me if I'm wrong):
One more change was added in Android 9: only apps running in the foreground (or a foreground service) could capture the audio input. 1
So below is an exceed of the official Foreground services documentation, omissions are marked with [...].
Declare foreground service types
[...]
If your app targets Android 11 (API level 30) or higher and accesses the camera or microphone in a foreground service, declare the camera or microphone foreground service types, respectively, as attributes of your component.
Note: Although adding a foreground service type gives a foreground service the capability to access location, the camera, or the microphone, this foreground service is still affected by the access restrictions that were introduced in Android 11.
[...]
Restricted access to location, camera, and microphone
To help protect user privacy, Android 11 (API level 30) introduces limitations to when a foreground service can access the device's location, camera, or microphone. The limitations depend on the app's state when the service starts.
Restrictions applied to services that start while an app is running in the background
On Android 11 and higher, if your app starts a foreground service while the app is running in the background, the service has the following access restrictions:
[...]
- The service cannot access the microphone or camera.
[...]
Restrictions applied to services that start while an app is running in the foreground
If your app starts a foreground service while the app is running in the foreground ("while-in-use"), the service has the following access restrictions:
[...]
- If the user has granted the RECORD_AUDIO permission to your app, the service can access the microphone only while the app is running in the foreground.
To conclude the above: If my foreground service is started while my app is in background, I can not record audio. If my foreground service is started while my app is foreground ("while-in-use"), I can only record audio while my app is foreground ("while-in-use").
So now my question is: Why would I use a foreground service if it has literally no influence on audio recording on Android 11+? I can just record audio, without a foreground service in my app to achieve exactly the same behaviour as with a foreground service. What am I missing?
If android:foregroundServiceType="microphone"
is set in the foreground service declaration, you can record audio while non activity is in foreground but only the foreground service is active. I did a quick test to confirm this behavior for Android 13 device with app targeting sdk 31.
I think if your app has an active foreground service it is considered running in the foreground and the restriction do not apply.