I have an app with an integrated music player. I don't want the music to be interrupted by incoming calls.
I use following function for that:
public static void updateDoNotDisturbMode(boolean enabled, boolean checkPrefs)
{
...
AudioManager audioManager = ((AudioManager) MainApp.get().getSystemService(Context.AUDIO_SERVICE));
NotificationManager notificationManager = (NotificationManager) MainApp.get().getSystemService(Context.NOTIFICATION_SERVICE);
// audioManager.setMode(AudioManager.MODE_IN_CALL);
if (!enabled)
{
audioManager.setStreamSolo(AudioManager.STREAM_MUSIC, false);
audioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, false);
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
...
}
else
{
audioManager.setStreamSolo(AudioManager.STREAM_MUSIC, true);
audioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
...
}
}
what works
silent phone if no headphones are connected
internal music player does play on without interruption if someone calls
so without headphones connected, everything works perfectly fine
what does not work
if headphones are connected, the standard beep sound is interrupting my music player and is played in the headphones
How can I avoid that incoming calls are interrupting my music player even if headphones are connected?
TARGET SDK
Only 4.2 and upwarts... (including 5)
What do you mean integrated music player? Can you change code of music app?
A player stops playing because it listens to PhoneStateChanges - void onCallEvent(int state, String number);
If you can change code of the player you should check is mute mode on within this method - if yes then do not stop playback.
If you can't change the code of player - you can use advanced player with preferences of audio focus. For example poweramp has such option. Set Settings/Audio/Audio Focus/Pause in Call to false (it works, tested right now). But you should change it manually all the time.
Related
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
We have this code to do a video call on Speaker. If user connects earphones, then we we are checking using the below code.
if(audioManager.isWiredHeadsetOn()) {
audioManager.setWiredHeadsetOn(true);
} else {
audioManager.setSpeakerphoneOn(true);
}
But the above code detects headset but audio is still on speaker rather than in earphones. How to fix this ?
And i want to know how to handle if user connects headset during a call. How to detect and send Audio through earphones at that time ?
We need to set the speaker to "false" before setting the wired headset "true"
if(audioManager.isWiredHeadsetOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setWiredHeadsetOn(true);
} else {
audioManager.setSpeakerphoneOn(true);
}
I received the following feedback when submitting an app for Android Auto:
Your app does not support all of the required voice commands. Volume
doesn't reduce when voice command is initiated on the Android Auto.
I take this to mean that the app should reduce playback volume (it's a media player app) when the user presses the Speak/Mic button in the Android Auto UI (or also if they scream "Ok, Google" over the sound of their music, I suppose).
I guess the other possible interpretation is that there are voice commands for raising/lowering the volume that need to be supported, but that seems...unlikely. And I'm not seeing any such API hooks documented anywhere.
So I assume it's the former case, and I need to reduce the volume when voice recognition starts.
To do that it seems like I'd need to receive a notification of that event (and preferably, also a notification of when voice recognition has ended). Is there a boradcast intent or other way to trap this in my app so that I can reduce the media playback volume while the user is trying to say things?
Found the solution. What the app needs to listen to are audio focus change events, like:
#Override
public void onAudioFocusChange(int focusChange) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
if (focusChange == AudioManager.AUDIOFOCUS_LOSS |+ focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
pause();
shouldPlayOnFocus = true;
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
if (! isPlaying()) {
return;
}
//reduce the playback volume
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
if (! isPlaying() && shouldPlayOnFocus) {
start();
shouldPlayOnFocus = false;
}
//restore stream volume if we reduced it earlier
}
}
This is covered in the reference documentation, here:
https://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
Android Auto triggers a transient loss of audio focus event (AUDIOFOCUS_LOSS_TRANSIENT) whenever the 'Speak' button is pressed.
I am implementing Quickblox WebRTC calling . All is successfully implemented, but I want the audio call through ear speakers of the phone. By default, it is only on loud speakers.
How can I enable calling through ear speakers like any other calling app? I also tried this:
AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
But still didn't worked for me!
To switch the audio socket you need to call the switchAudioOutput(); method on your current version
it switches between the loudspeakers and phone speakers (if the headset is on, then it switches between the headset and the loudspeakers)
If you are using QuickBlox SDK version 2.5.2, you can set the AudioManager like this:
audioManager = AppRTCAudioManager.create(this, new AppRTCAudioManager.OnAudioManagerStateListener() {
#Override
public void onAudioChangedState(AppRTCAudioManager.AudioDevice audioDevice) {
}
});
audioManager.setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
audioManager.setOnWiredHeadsetStateListener(new AppRTCAudioManager.OnWiredHeadsetStateListener() {
#Override
public void onWiredHeadsetStateChanged(boolean plugged, boolean hasMicrophone) {
}
});
audioManager.init();
I'm developing a game. I need a button to turn on and off game sounds.
In my app, I play background music which I want to be muted upon clicking a button. Here's my code:
AudioManager aManager = (AudioManager)getSystemService(AUDIO_SERVICE);
if (aManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
When I debug, the conditions are right but nothing happens! The sound is never turned off!
Check for method "isVolumeFixed()". If true, your device can't have it's sound changed. Some devices have a policy about that.
I'm developing a spam call filtering app and I'm trying to silence ringer for incoming (spam) call. The problem is none of AudioManager.setStreamMute nor AudioManager.setRingerMode is working in Lollipop. Here is my code snippet:
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
Log.i(TAG, "unmute");
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
Log.i(TAG, "mute");
}
}
When there's an incoming call, the mute part always gets executed but it sometimes succeeds and sometimes fails to mute the ringer. I can't find any rule. And audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) doesn't work either. This seems work fine when tested on emulators < 5, so I guess it's somehow related to Lollipop not having silent mode but interruptions filter. Commercial spam call filters are woking fine, so can somebody let me know how I could silence incoming calls with Lollipop?
I've got the same issue for android 5. AudioManager.setStreamMute with the value false to unmute is never working.
I tried AudioManager.setStreamSolo and it worked for me.
//to mute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, true);
//unmute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, false);
It mutes all other streams except one you want to play. In may case I needed to play my own audio instead of ringtone.
But you can try to play a silence audio to hack if you need absolute silence.