How to get mic data when other apps are using Mic?

Viewed 536

Microphone audio input sharing between multiple apps in android

I encountered this problem while working on my current app, in which I want to share the microphone audio between two(can be multiple) apps at the same time.

I tried recording audio data via AudioRecord class

audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
, 44100, AudioFormat.CHANNEL_IN_STEREO
, AudioFormat.ENCODING_PCM_16BIT
,pcmBufferSize);
//for reading data 
private ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); //define at class level
.
.
.
buffer.rewind();
audioRecord.read(buffer, buffer.remaining());

The problem is that when any other app starts using mic then we are getting silent packets.

As per official documentation:

Android 10 (API level 29) and higher imposes a priority scheme that can switch the input audio stream between apps while they are running. In most cases, if a new app acquires the audio input, the previously capturing app continues to run but receives silence. In some cases, the system can continue to deliver audio to both apps. The various sharing scenarios are explained below.

But I have seen many apps sharing the audio input without any issues. For e.g. Zoom, when a zoom call is ongoing, and I start an audio recorder then both apps get the audio.(though zoom audio decreases in intensity).

And another example is of Omlet Arcade, they are able to record mic audio even when mic access is given to other apps. Example: let's say I am streaming some battle royale game through Omlet Arcade, then my audio can be heard by my teammates(in the game) and also by the people who are watching my video stream.

So, it will be really helpful if the stackoverflow community could guide me towards a solution.

1 Answers

Not sure if you took a look just below the part of the documentation you quoted, but it specifies in the next section why some apps could share audio input:

There are four main [sharing] scenarios:

  • Assistant + ordinary app
  • Accessibility service + ordinary app
  • Two ordinary apps
  • Voice call + ordinary app

Voice call + ordinary app:
A voice call is active if the audio mode returned by AudioManager.getMode() is MODE_IN_CALL or MODE_IN_COMMUNICATION.
Android shares the input audio according to these rules:

  • The call always receives audio.
  • The app can capture the voice call if it is a privileged (pre-installed) app with permission CAPTURE_AUDIO_OUTPUT.
  • The app can capture audio if it is an accessibility service.

To capture the voice call's uplink (TX), downlink (RX), or both, the app must specify the audio sources MediaRecorder.AudioSource.VOICE_UPLINK or MediaRecorder.AudioSource.VOICE_DOWNLINK, and/or the device AudioDeviceInfo.TYPE_TELEPHONY.

Further down again, it specifies how you can set a privacy sensitivity level in Android 11 for the recorded audio, e.g. setting it to 'insensitive' allows the audio to be shared with other apps. Hope this helps understand where to start

Related