Android internal microphone release issues
Asked Answered
J

3

20

I have a very interesting problem.

I was under the impression that when my app uses the microphone, and the Android system process (like incoming or outgoing call) needs the microphone, my app releases it without any consideration.

But i guess that is not the issue anymore, since i have received few complains that when the app is being used and there is incoming call, the other side on the call, can not hear them.

Is there any way to assure that whenever system process requests the microphone, to be released?

Jahveh answered 15/8, 2016 at 17:47 Comment(3)
try #4194842Canyon
Hi Pavneet. So from what i read on the link provided, just certain brands in certain regions do not solve this problem. The rest, automatically requests the microphone and any app holding it, releases it right?Jahveh
And to add to that, my question is, since my app is not a recording app, is there any way to assure that the app will release the microphone to system processes?Jahveh
S
5

Currently, there is no way to check whether another app requests the microphone. However, there are some workarounds:

  • If you record sound in an Activity, simply release the microphone in onPause(), so that the foreground app can use it. This won't work if you record audio in a background service.
  • Pause the recording when there is an incoming call. To detect incoming (and outgoing) calls, take a look at this excellent answer. When the call ends, resume the recording.
  • A dirty hack: If detecting incoming calls isn't enough, you could loop through all running apps and see if they have the microphone permission. As soon as an app with microphone permission is running, pause the recording. Getting all running apps can be difficult. For more infos, look here, here, here and here for checking the permissions.
Simmons answered 18/8, 2016 at 8:43 Comment(11)
Thanks Manu. My app needs the microphone while in background, because it tries to recognize audio even then, of course if the user wants that. As for the incoming calls and outgoing, i have already implemented the solution and it is tested it works. As for the dirty hack, i was trying to implement something like that, since there is no other option at the moment, just what i was thinking is to check just the app in the foreground if it has RECORD_AUDIO permission i stop the use of the microphone. But would it not make too much processing like this if i run that on a timer of 10-15 seconds?Jahveh
@BorceIvanovski I've developed a simliar app which checks all running apps every 2 seconds and it doesn't drain too much battery. You could optimize it further, for example you don't have to check for running apps if the screen is off and so on...Simmons
ok, i didnt thought about that part, to optimize it even further. If that doesnt drain the battery at every 2 seconds, than that might be even better option.Does anyone know any example how to get the current foreground app package name? Since i dont need to look for running apps, i just need to look for which one is in the foreground, since doesnt matter whether it is running, it will have to come in the foreground in order to be used.Jahveh
sorry i just went through the links you provided, and they give the information of what i need, let me try it and see if i can make it work with it.Jahveh
sorry for late reply, i just managed to implement the solution. The suggestions are working as described in the links you gave me. I just have one more issue, for some reason, when i use the function from the link getForegroundApp(), since i am working a lot with Asian mobile phones, i am getting package name, but also some weird symbols, and i tried removing them, but no luck. Any suggestions? Here is a screenshot prntscr.com/c9nhth so i am trying to achieve check for the permissions if they are granted for the specific foreground appJahveh
You could remove all non-ASCII characters from the string... This answer may contain what you are looking for: https://mcmap.net/q/204498/-how-can-non-ascii-characters-be-removed-from-a-stringSimmons
perfect, i saw some similar post, but didnt had chance to check it out, but i thought let me thank you here, and ask maybe you know, as you knew :) thanks a lot for the help Manu.Jahveh
unfortunately that didnt work, the strange symbols are still in the string :( anything else i can try, anyone can think of ?Jahveh
ok solved, by using .replaceAll("\u0000",""); Thank you everyone for the helpJahveh
i have another problem, the getForegroundApp function in one of the links, works on most of the phones i tests, but i got an issue with Galaxy S7, it doesnt bring me the foreground app package, it just gives me this all the time, doesnt matter whats in the front: com.samsung.android.providers.context Any ideas?Jahveh
@Jared Rummler, is it possible to see why your function doesnt work on API 23?Jahveh
C
0

As far as I know, there is no way to know the microphone's state is Busy, Available or if somebody is requesting for it because it's somehow these are responsibility of system to manage processes and resources so keeping this in mind , you can't keep the microphone forever and there so no way you can control it mean you can't mange and detect the request on resources .Following there is some info that might help you.

What you can try is , overcome the all possibilities when system can use microphone like setup a broadcast receiver for call broadcasts and when they triggered release the microphone and when call is done then you can acquire it again.

Canyon answered 16/8, 2016 at 5:11 Comment(2)
Thanks Pavneet for the info. I am not trying to achieve to keep the microphone forever, actually i want to achieve the oposite. Whenever someone is requesting the microphone, to release it to them. I have run into an issue in some Android devices in Asia, where when the app is working, incoming or outgoing calls dont get the microphone, which i thought it is impossible, since they are system processes. And yes i follow the approach, but some users might keep the app in the background for longer period of time, which is still using the microphone.Jahveh
so this is why i asked this question, to see if i can optimize the app, and release the microphone when it is in background and some other app is requesting the use of the microphone. or to make sure when system processes request the microphone to be released doesnt matter at what stage my app is.Jahveh
D
0

In my case I created background thread that checked incoming bits from microphone. If they would zero (or several seconds data lag) the app interrupt the process. Nevertheless, of course, this is not a "good" solution but might be helpful.

Discernible answered 17/8, 2016 at 19:2 Comment(1)
Thanks Vyacheslav, but that wont be of any good to me with this problemJahveh

© 2022 - 2024 — McMap. All rights reserved.