简体   繁体   English

如何在Android中完美使用MediaPlayer

[英]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. 我在一个在线广播流媒体项目中工作,在该项目中,我正在通过URL与Mediaplayer播放音频。 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) (因为android的默认音乐播放器有效)

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. 但是我所做的仍然可以达到我想要的命运,但是却缺少了一些东西,比如我已经在onResume开始播放音乐并在onDestroy停止了播放音乐。通过这样做,当我按下主屏幕按钮时,音乐会一直在后台播放,但是当手机来电并收到后,它继续播放,这造成了令人作呕的情况,但是当另一个应用程序打开时,音乐便消失了。 How should I manage the MediaPlayer that will work like what I want? 我应该如何管理可以像我想要的那样工作的MediaPlayer Any suggession? 有什么建议吗?

Note: I am not using any service here, just an AsyncTask class to play the music. 注意:这里我没有使用任何服务,只是一个AsyncTask类来播放音乐。

The topic of MediaPlayer as background service is covered in multiple sources such as this one. MediaPlayer作为后台服务的主题涵盖在多个来源中,例如这一来源。

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

You can also find this in the Android official documentation here 您也可以在Android官方文档中找到此内容

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

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM