9

I'm using code from the following URL in order to get sound data from the microphone with AudioRecord: http://www.dreamincode.net/forums/topic/303235-visualizing-sound-from-the-microphone/

The problem I'm seeing is that if another app is also using the microphone, I'm getting an error (status -3) from AudioRecord. Everything works as expected if I kill the other app which is using the microphone. Is there an elegant solution to allow my app to access the microphone even if another app is already using it?

Thank you.

codeman
  • 8,868
  • 13
  • 50
  • 79
  • Some more information: AudioRecord.read is returning -3, which I believe means that it read zero bytes. – codeman Jun 11 '14 at 19:40
  • Do you want to *switch* which app is using the microphone, or allow them both at the same time? If the latter, this is a duplicate of [Share Audio Input?](http://stackoverflow.com/q/9758946/752320) Simply put, only one app can use it at a time. – Geobits Jun 13 '14 at 20:10
  • I want to switch which app is using the microphone. I assumed that the app in the foreground would get control of the mic, but it seems that whichever app has it first will keep control. Thanks for any help. – codeman Jun 13 '14 at 20:20

2 Answers2

10

Sadly, only one app at a time can access the microphone (just like the camera).

Also, there is no standard way to inform another app that you want access to the microphone (so that they release the resources and let you access it). You could however send a broadcast to all other apps ("requesting the microphone"), but the other apps would have to implement this feature (and very few or zero developers will do this).

I would recommend you to simply inform the user that the microphone is currently not available, because you can't do anything else.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • 1
    Thank you. I ended up just informing the user that it can't access the microphone like you said, and to close any other apps that are using it. I have it restart access to the microphone once they come back to my app. – codeman Jun 16 '14 at 14:28
  • How do I become the other app here? If i want to implement the feature when i receive a broadcast to relinquish the microphone resources ? – Lena Bru Jun 15 '15 at 08:11
3

If you are using the "OK Google" function with the option to access it from any page, try turning that off (the 'any page' bit From Google app screen MENU>SETTINGS>VOICE>"OK GOOGLE DETECTION">FROM ANY SCREEN=OFF): it hijacks the microphone, or can do.