how to work with a MediaPlayer perfectly in android

I am working in an online radio streaming project, where I am playing audio with mediaplayer from an URL. The music is playing and working fine. But I am stuck with the play,pause functionality regarding to the state of the player activity. I want such that when the app will be exited by home button press then music will keep playing, but when a phone call rings then the music will be paused and after the phon call the music will resume. And also when user opens another app then the music will continue playing in background. (as the default music player of android works)

But still what I did results almost my desired destiny, but something is missing, like I have started the music in onResume and stopped the music in onDestroy .By doing this the music keeps playing in background when I press home button, but when a phone call comes and recieved it continues playing which creates a disgusting situation, but when another app is turned on then the music goes off. How should I manage the MediaPlayer that will work like what I want? Any suggession?

Note: I am not using any service here, just an AsyncTask class to play the music.


The topic of MediaPlayer as background service is covered in multiple sources such as this one.

http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

You can also find this in the Android official documentation here

http://developer.android.com/guide/topics/media/mediaplayer.html

Hope that helps.

链接地址: http://www.djcxy.com/p/41794.html

上一篇: 应用程序关闭时继续播放音乐

下一篇: 如何在Android中完美地使用MediaPlayer