I have implemented a VoIP application and everything works fine. When a user is playing a music on his device using Android default music player or any other third party apps, and my app starts to ring or a message is received, my app's ringtone is not played and the music player is still playing and this situation keeps happening even in incall and call termination states.
How do I detect when a music player (default player for this stage) is playing a music and how to pause and resume it on my app's ringing and call termination states?
An application should request Audio Focus when it needs to utilize the audio output, either with music or notification sounds. Once it has the priority of Audio Focus, it can use the sound output freely, while listening for focus changes. If Audio Focus loses the focus, it should immediately either kill the audio or lower it to a quiet level and only resume loud playback when it receives focus again.
If an application needs to output music, Audio Focus should be requested. The method requestAudioFocus() should be called from the AudioManager.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus( this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// could not get audio focus.
}
Take a look on Handling Audio Sound from Overlapping with VOIP call
Related
I'm working on an app allowing users to watch videos. When they open a video to watch, I call:
AudioManager mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
This stops playback from any other apps streaming music at the time. I've tested with Play Music, Spotify, Soundcloud, etc and they all stop music playback at this point.
When the user is done watching a video, I call
mAudioManager.abandonAudioFocus(this);
but the app that was streaming music and previously paused does not resume streaming music. How can I get this to work?
I've tried doing AUDIOFOCUS_GAIN and AUDIOFOCUS_GAIN_TRANSIENT. Everything I've read on Stack Overflow just says I just need to release the audio manager. This seems so simple, but I can't figure it out. I want the functionality to be similar to how Instagram pauses and resumes music when you view a video.
As you are requesting the complete 'AUDIO_FOCUS', the external application has to hit the play button to request the 'AUDIO_FOCUS' back. Just because your application has gained the 'AUDIO_FOCUS' and then abandoned it, this wouldn't resume the previous music/video which was being played.
The only way to overcome this would be to rethink the requirement of the application. Therefore, you can request temporary audio focus, i.e. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT (or AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_CAN_DUCK depending on whether or not you think it's ok for other audio to keep playing in the background at a lower volume).
int requestAudioFocusResult = audioManager.requestAudioFocus(TimeWhisperService.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if(requestAudioFocusResult == AudioManager.AUDIOFOCUS_GAIN) {
//ACCESS GRANTED
}
In my player app (on Android 6.0) the music volume (of my own player, not the media player api) remains low/ ducked after the audio focus returns to the app.
I'm not ducking anything myself when the audio focus gets lost, just stopping and resuming the playback on the respective event types AUDIOFOCUS_GAIN / AUDIOFOCUS_LOSS of my OnAudioFocusChangeListener implementation.
I have already tried to explicitly set the stream_music volume to max or to call
adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
but with no success. My app needs to be restarted then the volume level is normal again.
Whereas the Android media player behaves correctly, it raises the volume after a phone call is finished. It is just with my app.
How can I force returning to normal volume programatically ?
In an android application, I have written a service that plays music at the background. I have implemented OnAudioFocusChangeListener that listens to change in audio focus. The problem is I dont recieve a call back to onAudioFocusChange() in the following situation.
Play music in my app and put the app in background. (Music keeps playing at the background).
Start youtube app, and play any video. This gives a callback to onAudioFocusChange() where I pause my media player.
Now I press home button for youtube(youtube stops playing).
Go back to my app and play music again and put the app in background(music keeps playing at background).
Now start youtube and resume the video(stopped earlier), This does not give me a call back to onAudioFocusChange().
Note : Only resuming a video does not give a call back to onAudioFocusChange(), but playing a new video, gives a call back to onAudioFocusChange().
Found a solution for my own problem. This is how I solved the issue.
Every time your application loses audio focus, your have to request for audio focus and then play music. This will allow you to listen to the changes in audio focus through onAudioFocusChange().
public boolean requestFocus()
{
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager
.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
Is it possible to mix my app's audio with the music player audio?
I find that by default, android does mix the audio of the music player with my app.
However this stops working after calling AudioManager.requestAudioFocus method. Meaning, if the music player is playing, when my app starts playing, it stops the music player. I want them to play at the same time.
I request the audio focus so I can know when other interruptions such as phone calls have happened. I do want to stop my app's audio when a phone call happens.
My audio plays sin waves, it is a medical device. I request the audio focus with AudioManager.STREAM_MUSIC and AudioManager.AUDIOFOCUS_GAIN.
I am using AudioTrack to play them.
Before open my application I played some of the music from default music player. With that background music I opened my application with the mediaplayer (android.media.MediaPlayer) to play the mp3 available in assets.
The default doesn't stop the music and I am getting two music concurrently from the device.
How do I stop or pause music of the default music player whenever my app started playing the music?
I think thats impossible. The default music player is a different app and out of your control. android does not allow controlling other app features from your app.
I had the same problem. I was trying to stop the background music of the default music player (if it is playing) when my VoIP SIP application receives an incoming call. I came up with:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
This does not stop or pause the music, but it make the music stream silent. Do not forget to unmute it later. This solution works well only if your second sound does not use the same stream.