search

Home  >  Q&A  >  body text

java - Android 有关于后台播放音乐的问题

最近在做一个音乐播发器,我把播放音乐这个功能写在一个服务里,再和播放的界面进行绑定,注意是BindService而不是StartService。当我播放界面finish()后并且按理说服务也自动销毁了,但是音乐仍在播放,我用的是MediaPlayer类播放的音乐,不知道为什么,求大神告知。

PHPzPHPz2887 days ago461

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 17:38:33

    Did you call unBind before finish?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:38:33

    BindService will not be destroyed as long as there is another activity bound to this service. It must wait until all activities are called unbound() before it can be destroyed.


    Service life cycle diagram

    A practical experience is to send a message to the Service before the activity's unBound() to tell the service that the activity is to be unbound. This blog post has some source code and an introduction to Android BoundService inter-process communication

    In addition, does Mediaplayer call release() in onDestroy of service?

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:38:33

    Only the current playback interface is bound to it, and the mediaplayer is not released in the service. But logically speaking, since my activity has been finished and the bound service has been destroyed, the mediaplayer in the service should also be destroyed.

    reply
    0
  • Cancelreply