How to stop music in my service when youtube app resumes video?
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);
}
链接地址: http://www.djcxy.com/p/57456.html
上一篇: 继AVAudioPlayer后播放音乐