I am developing one app in which call functionality also available but when i want to add speaker on and speaker off functionality like Whatsapp while making calls. can anyone tell me how to achieve this in android. thanks in advance.
speakerOn.setOnClickListener {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL)
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(false);
mediaPlayer.start()
}
speakerOff.setOnClickListener {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
mediaPlayer.isLooping
}
I have a test app that uses Google voice in a continuous manner and it plays the beep sound every time Google recognition service is called. I am trying to get rid of the beep sound. I have read threads of muting the music stream but that would not work for me.
I am trying to find the beep file location so I could just go and delete it from the system. I followed this thread, but I cannot see the file in 5.0 system file.
Assuming you don't want to mute all streams because you are interested in playing your own sound, this might be a solution for you: Use the Audio Focus API.
So you would have something like this:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Play your own sound (or play silence)
}
am.abandonAudioFocus(afChangeListener);
I have not tested, whether the Google App (which plays the beep sound) adheres to this request, but generally it should work and its worth giving it a try.
Another option would be to directly mute all sounds coming from the Google App (which includes the Google voice recognition beep sound). The advantage of this method is that there is no interference with any other streaming audio. The following is a link for further detail. Do note, however, that this requires root access:
https://android.stackexchange.com/questions/128588/how-do-i-disable-the-google-voice-typing-voice-search-ding-sound.
The only solution I've found to mute beep sound recognition is:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Before you should save the volume, for example:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
current_volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
To return to restore the volume level, such as tapeworms:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
I hope it helps you on something
You can use audio manager to control the sounds
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
before you start recognizing call
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
and after recognition is complete
call
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
I found another solution witch works fine for me. I set the audio stream of the media player to another stream, so i could mute the music stream. I hope that helps somebody else too.
afd = context.getResources().openRawResourceFd(R.raw.duduuudududu2);
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
myAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
try
{
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mp.prepare();
}
catch (IOException e)
{
e.printStackTrace();
}
mp.start();
Is there a way we can check programmatically Which Music player is playing right now?
like Google Play, Samsung default Music Player, any 3rd party music player
Actually, we need to programmatically handle play/pause of music player. Google Play and Samsung Music works differently with code :
// Google Play do not play pause with this code
// it is using different package name i guess
CmdStop = "togglepause";
i = new Intent("com.android.music.musicservicecommand.togglepause");
i.putExtra(CmdName, CmdStop);
context.sendBroadcast(i);
Any help is appriciated
Thank you
you don't need a broadcast receiver for this - AudioManager is your friend:
AudioManager.isMusicActive() does the job you want, have a closer look here for details: AudioManager
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or do it not
}
stackoverflow answer
I am not able to mute the incoming call ring tone and play my own media file using MediaPlayer class.
So far I have tried the following in my Activity that pops on top of incoming call screen, started using a BroadcastReceiver; Activity shows up but audio doesn't play:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_RING,true);
am.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
Then I start playing the audio file. The sound plays perfectly in normal mode, so I also set the audio mode to MODE_NORMAL using am.setMode method but no luck.
Is there any way around? I am currently mobile and so I could not post full code. If anyone has done this before, a block of code would be a great help. Moreover, I dug up the documentation for AudioManager class and found setRoute or similar method which looks good but it has been deprecated long ago so I didn't want to implement it.
Does this approach have different reaults when screen is on or off during an incoming call?
Can anyone please show me how and where can I modify the Phonegap source to play audio through the earpiece?
Try this code:
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
Also need to add new permission:
MODIFY_AUDIO_SETTINGS
Either put this code in your activity start (onCreate()) or you need to write a plugin to enable/disable it based on your requirement from the javascript.
Check this post for more
Android - Getting audio to play through earpiece