Tuesday, 15 September 2020

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

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.



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

No comments:

Post a Comment