music pause when app killed when using service

后端 未结 3 1589
無奈伤痛
無奈伤痛 2021-01-29 00:57

I am using a Service class to play music in the background. While I am displaying the notification bar on top, there is issue in my app. When I kill the app, the mu

相关标签:
3条回答
  • 2021-01-29 01:13

    According to: https://developer.android.com/reference/android/app/Service.html Change

    START_STICKY
    

    to

    START_NOT_STICKY
    

    EDIT:

    The issue with music stopping for a while is connected with the fact that you run app and service in the same process. And 'killing app' means stopping the whole process bound to it. What you need to do is to run your service in seperate process:

    Start a service in a separate process android

    0 讨论(0)
  • 2021-01-29 01:15

    That tutorial shows you how to build a BACKGROUND service, which can get killed by the system. To play music you should use a FOREGROUND service.

    https://developer.android.com/guide/components/services.html#Foreground

    0 讨论(0)
  • 2021-01-29 01:21

    Use onTaskRemoved on your main activity and release your mediaplayer on this event.

    0 讨论(0)
提交回复
热议问题