Release AudioRecord android when other app request for recording
Asked Answered
D

3

10

I have an audio recording service in my app which will record the sound continuously. So, it will always occupy the AudioRecord. It means no other app can use audio recorder as it is already occupied by the service. Is there any way to notify that other app is requesting for audio recorder(so that I can release it) and also when the app releases it(so that I can assign it back to the service)?

Draff answered 5/9, 2014 at 9:27 Comment(4)
I think that is no common senario, so I guess there is no way to achieve that.Improvement
@Improvement The calling feature of device takes the mic automatically even if the service is running. So, I guess there must be some way.Draff
I think that uses lower native APIs which are not aviable for any apps.Improvement
@Improvement That may be possible! But still I will look forward for someone to answer ^^Draff
B
1

Maybe a possible way is to create a BroadcastReceiver which receives an event from the app which is requesting the control over the mic source. The onReceive() method should interact with the service and release the resource. When the other app is finishing it can revert the process to start the service again. If you can't get control over the behavior of the requesting app I think there's a slightly different problem. Anyway:

The problem is all about knowing when the resource is being requested, this can be done through AudioManager intent types. Be sure to check Managing audio focus which talks about audio focus loss in TRANSIENT way!

Burro answered 23/9, 2014 at 9:15 Comment(1)
I read AudioManager class, but there are no broadcasts for Mic availability. There are some flags instead, which I don't think can be used in my case.Draff
A
1

As @Rekire mentioned, there is possibly no way to achieve this. Also, AudioManager provide no such broadcasts, so it is not possible for different apps. Maybe rooting the device is the only option.

Ancillary answered 25/9, 2014 at 7:52 Comment(1)
Hmm.. guess I have no other choice!Draff
Y
0

This can be done with AudioManager.OnAudioFocusChangeListener callback. Just stop recording on AUDIOFOCUS_LOSS_TRANSIENT event and start again on AUDIOFOCUS_GAIN event. This solution works well for Google Voice Search (Google Search widget, Google Chrome, etc).

But unfortunately it works poorly for other ordinary applications (for example HTC M7 Voice Recorder app is not able to start recording on first click on "Record" button, second click do the trick - it seems app should be ready to retry recording on failure several times).

Yingling answered 21/5, 2015 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.