how to mute microphone in android by java programming - android

I am working on project that needs to mute the android microphone to avoiding record audio by any other apps.
I'm trying to googling and i found how to mute microphone but the articles just mute the microphone on during a call
code :
AudioManager audM = (AudioManager) m_instance.getSystemService(Context.AUDIO_SERVICE);
audM.setMicrophoneMute(true);
I need an article to mute the microphone on device that other apps can not record at all.
please help me to do that

Android AudioManager, provides all methods needed to play with mic. but your question has to do with a "global" hardware setup - not inside your app - so if Android OS does not provide that functionality, I think the only think you can do is directly blocking the mic to other apps, or block other apps while your code executes .. did you search APIs that does that ?
As for below thread, it seems that if your app initializes the mic, it wont be available for other apps to use it, I didn't check that.
Here is the thread:
How to block the mic for another app to use?

Related

Losing Audio while connect to Bluetooth SCO

I know this is a very specific issue, but I'm hoping someone else has some insight.
I am using a third Party TTS (Text to Speech) and ASR (Auto Speech Recognition) library from Cerence. I've noticed on Android 12, and some Android 11 devices, when I use them together is will shut down the Audio output. I can still use the ASR for input, but no audio will be played.
I'm not seeing much in the logs for this, but when this happens, I need to disconnect the BT headset and reconnect it.
In minimal testing, Shortly after enabled Bluetooth SCO, the Audio is lost on Android 12.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// Need to set mode so we continue to get the Audio in the BT headset.
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.startBluetoothSco();
A similar thing happens on Android 11, but while I can make it fail reliably, I don't know a precise way to make it happen.
I'm wondering if anyone has any insight on how to debug audio issues. Is there certain logs I can pull or look for? Is there someway I can troubleshoot audio issues? When this happens I can't play audio in any way, as an example YouTube no longer has audio in the headset. So either the audio is being redirected somewhere else, or it has crashed.
I'm also wondering if there is another way to take control of the mic outside of Bluetooth SCO, or if there is a SCO setting I should be using that acts differently.
UPDATE:
In more testing I've realized that the AudioManager mode is being changed by something from MODE_IN_COMMUNICATION to NORMAL. I have not figures out why yet, but if I keep forcing the mode to MODE_IN_COMMUNICATION, my audio continues to be played.

Why it is not possible to play an audio file on a voice call in android

This question might seem to be a repetition of the questions such as following:
How to play an audio file on a voice call in android
Background Audio for a Call in Progress - Possible?
The answers of these questions suggests that it is not possible to play a pre-recorded audio on a voice call in android. I want to know why it is not possible? What is the limitation (hardware/software)? Is it really a limitation or done purposely? Can we alter the source code of android to make it possible?
I think this is a limitation, imposed for security reasons and restricted at the OS level.
Let's analyze the security threat, first of all. If you were able to play custom audio files to the callee, a whole world of cons opens up: you could trick customer supports, you could pretend to be someone else, you could give unauthorized purchase confirmations, and so on. For this reason, neither Android nor iOS allows this functionality.
On Android, you won't be able to do so in a programmatic way, simply because the current APIs won't allow you to do so. It is stated in the official documentation as well, as pointed out here. If you dig into the source code, you can probably enable this feature by accessing the microphone output during a phone call, but that would require running your custom version of Android. A good starting point would be the AudioTrack source, available here.
EDIT: a good example of an audio mod involves enabling the Nexus 5 earpiece as a second loudspeaker (requires root). Can be found here.
After a thorough research, what I have come to know is that there are more than one limitations/hurdles to make it possible. These limitations/hurdles are at three different levels.
First limitation is at API level, because there is no high-level API to play sound files in the conversation audio during a call as mentioned in Android official documentation.
Second limitation is at Radio Interface Layer (RIL). RIL passes on complete control of the call to Radio Daemon (rild) of the Linux library which then further passes the control to the vendor RIL. That means we cannot manipulate voice call in android source code.
Even if we are able to remove these two limitations, we may still not be able to play audio file to an ongoing voice call. Because there is a third limitation. Every vendor has their own library of RIL that communicates with Radio Daemon (rild). This requires that vendor RIL to be open source which is not actually. Hardware vendors do not usually make their device drivers code available.
Detail discussion on this topic is present at this link.
This is software related due to the prioritization of audio routing in Android.
Take a look into the CallManager where you can dig into the method setAudioMode(). After the audio mode was set to MODE_IN_COMMUNICATION the following code is called
audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
From this point on the telephony service has the highest priority and won't let any other audio play in parallel.
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.
See official link
http://developer.android.com/guide/topics/media/mediaplayer.html
By implementing the AudioManager.OnAudioFocusChangeListener you can get the state of the audiomanager. so by this if any music is playing in the background you can get the AudioManager states(playing and pausing is completely in developer hands) similarly......
Some of the native music players in android device where handling this, they restrict the music when call is in TelephonyManager.EXTRA_STATE_OFFHOOK.so this scenario is also completely in developer hand (whether to handle or not) if he is not handling both will play parallel y

How to bring audio from microphone to speaker on Android?

My problem is when I talk into the microphone, I want my sound to go to speaker of the phone which has Android OS. I think "MediaPlayer" class can't do it. Do you have any way?
Here you are not clear in which scenario you are going to do this but however it is possible to do using AudioManager, you can set your modes and audio services. For more details refer this link :
AudioManager

Set the volume control stream from a service on Android

I'm working on a music app which consists in a Service playing music while other apps are running.
In order to enjoy the music and only the music, the Service mutes all the other apps. In order to do that, it mutes STREAM_MUSIC and play music over STREAM_VOICE_CALL (found that solution to mute other apps here)
As the Service uses STREAM_VOICE_CALL to play music, what I'm trying to find is a way to make the volume buttons control this stream when a sound is playing.
What I already tried:
setVolumeControlStream: only works in Activities
Capturing volume keys pressed events: Services do not receive such events (source)
audioManager.setMode(AudioManager.MODE_IN_CALL) : does not work in my case. Plus, the SDK documentation states that this "mode should only be used by the telephony application when it places a phone call".
At this point, I'm out of options and any help would be appreciated. Thanks!
I would personally not suggest you to STREAM_VOICE_CALL as it is meant to be recommended to use it for Voice calls as compared to media playback. Using this may prevent you from using features exposed by Android for media playback.
Having said this & taking into consideration your use-case, I think you need to look for changing 'call volume' level as compared to 'media volume' level.
Have a look at the MediaPlaybackService.java as used in the stock Music Player app and try to customize it as per your needs.
I havn't tried it myself. but looks like it should work. Kindly try it out.
m_audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
m_audioManager.setMode(AudioManager.MODE_IN_CALL);

Android: Manipulating voice in phone calls

I'm currently searching for options on how to manipulate audio on android. The goal is to process audio from the microphone in real time during a phone call. The best solution would be to do this on a native call. But rebuilding a telephone app (no VOIP) would be fine too. Are there any ways to achieve this with Android APIs (also undocumented)?
If not, which steps would be necessary to get things running?
On iOS there are some apps which manipulate voice but create a VOIP connection. I heard that on Android you can "clone" the telephone app and eventually feed it with your own audio stream? Aren't there apps which add noises during a call? What kind of APIs are involved?
The best solution would be to do this on a native call.
This is not possible. You have no access to the in-call audio stream, except perhaps in speakerphone mode.
But rebuilding a telephone app (no VOIP) would be fine too.
The last official word from Google (2010), the entire OS has no access to the in-call audio stream, as it is all handled at a lower level. Even if newer versions of Android do have access to the in-call audio stream, "rebuilding a telephone app" is only possible if you are creating custom firmware.
As a drawback i could imagine to record the Downlink Stream using the MediaRecorder API and write it back to hardware using AudioTracks write() method. By this i could manipulate incoming voice. But still I think this wont work during phone calls. And I do not see a way to choose different hardware destinations.

Categories

Resources