Unable to access microphone when another app is using it in Android - android

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.

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.

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.

Related

Is there any way in Android 12 to observe Camera and Mic toggle state to prevent stream blank frame or silent audio? [duplicate]

With the new Android 12 operating system, the user now has the ability to completely disable microphone and camera usage from ALL apps. This is actually a good thing, letting users easily control privacy.
If an app starts and has permission to use the microphone and the camera AND the user has disabled all access to either of these hardware, Android 12 automatically pops up an alert asking the user to re-enable the microphone or camera, or to continue with the hardware disabled for the app.
Also, on creation, the app can simply check when starting to see if the needed hardware is enabled, and can also prompt the user further or handle the condition as the app sees fit.
But what if the user disables the microphone or camera while the app is running? I can't seem to find any interfaces for something like onMicrophoneEnabled() or even a Broadcast of such an event.
It's possible to check every time onResume() is called, but I was hoping there'd be a more elegant solution.
If anyone knows of something that my app can use to capture this when the user has made changes either way, please let me know.
After carefully reading the google docs (https://developer.android.com/training/permissions/explaining-access#toggles) on their implementation of the user enable/disable of the camera and microphone I discovered something very interesting:
Apps are explicitly forbidden from knowing if the user has done any of these actions. So of course there is no way to get notified of these changes: it is by design.
If the user turns off the camera this way, the app will still get a camera feed, but it will be black. Similarly the microphone will still work, but we'll get only silence. This is a feature--a way to ensure privacy.
The operating system will remind users that they have disabled the microphone or camera and provide a chance to re-enable the hardware, which is nice. But the app is completely in the dark (hehe).
Another way to think of it: Just like putting a piece of tape over your camera or your thumb over the microphone. Only this time it's done by the operating system. This way a spy program or some other nefarious software will still think it's working and not try to bypass or give up.
This also explains why testing during onResume() or onWindowFocusChanged() didn't work either. I was getting results that didn't align with the settings (they aligned with current permissions).
So to answer my question: There is no way to be notified. That is the point.

Call bot - how to?

We are looking to make an application in which a bot answers the call for us and says a predefined text like one sentence or two and the other person on their phone says/tells their work to the bot. Then after the call bot gives a speech to text.
As per my research we can automatically pick call in Android Oreo using android.permission.ANSWER_PHONE_CALLS
But I don't know further steps. Can anyone help me?
Actually this is almost impossible and very unreliable. Most phones don't have access to line audio since it is illegal to record calls in most countries.
This is the reason why most voice recorders can't record incoming audio in a call, at best they use the microphone to capture the audio coming from the speaker and some of them even require the phone to be rooted.
The permission android.permission.ANSWER_PHONE_CALLS only allows you to automatically "Pick up" the phone, but does not give you access to the call audio.

How to let other apps take over microphone access?

Is there any way to know when another app is trying to access the microphone so i can let the other app use it?
My current app keeps recording in the background but it blocks other apps access to the microphone, is there a way to let other apps take over microphone access?
It all depends on your use case. Theoretically you cannot "borrow" the permission from other apps for security reasons but nothing would prevent the app that already holds the permission to expose recording as "service". I do not mean Android Service here, but rather ability of letting other apps to ask your app (i.e. via special Intent or any other way) to do the recording and then return recorded data back.
And as already mentioned, keep in mind, this won't work on Android P as no longer available when app is not in foreground.
And we talk about non-rooted devices, because on rooted, you may do not care the permissions that much.

Auto call and play an audio recorded message

I wish to make automated calls to a list of numbers to then play a recorded audio message to them. The ones that don't pick up get a call back later.
What's the cheapest and easiest way to develop this using some pre-existing tools? Any directions welcome.
Note: You can play back the audio data only to the standard output
device. Currently, that is the mobile device speaker or a Bluetooth
headset. You cannot play sound files in the conversation audio during
a call.
from the official docs
I ended up using automated flow apps (Automate [link below] is one such app that I used. Disclaimer: I've no affiliation with app) to trigger automated calls. The flow was initiate on a GCM notification to the phone, which provided the number for call. With all permissions granted and conditions fulfilling (network, sim, call credits, etc.), app should be able to make call to the number.
Now, I'm trying to figure out how to play an audio message for the receiver.
So, if you were looking for 'how' part, here's the how part. If you know the next steps, enlighten me!
https://play.google.com/store/apps/details?id=com.llamalab.automate&hl=en&gl=US

What permissions are needed to programmatically mute a phone call, from a background app?

I have an Android app that happens to be running in the background on the user's Android phone, while the user is speaking on a phone call that someone placed to the user. At a certain point, my app would like to silence the audio from the phone call and play its own audio. Moreover, I'd like to do this without disrupting the user's speech.
What permission does my app need to have, to do this? Is there any permission that's available to third-party apps that is sufficient to let my app do this?
It looks like AudioManager.setStreamMute(STREAM_VOICE_CALL, true) will mute the audio output from the voice call. But I can't tell what permission this needs: it's not listed in the Android API documentation, or in the Pscout permission map. Also, I can't tell if there any restrictions on using this API from an app running in the background, on any version of Android.
(If the problem statement wasn't clear: Alice has installed my app on her phone. Bob calls Alice. Alice picks up the call and is talking to Bob. At a certain point, my app wants to completely mute Bob's voice and play its own audio clip instead, so Alice hears the audio clip instead of anything that Bob says. But, I don't want to turn off the microphone on Alice's phone: if Alice is saying anything, that should still be picked up over the call and Bob should still hear it on the phone call.)
You can try: android.permission.MODIFY_AUDIO_SETTINGS.

Categories

Resources