How to stop MediaLibraryService:media3 android
Asked Answered
L

2

0

I want to stop a foreground service which extends MediaLibrarySession only after clicking a specific button on the activity, so i tried :

val serviceIntent = Intent(this@PartyHostActivity, PlayerService::class.java)
stopService(serviceIntent)

and

val serviceIntent = Intent(this@PartyHostActivity, PlayerService::class.java)
startService(serviceIntent)
stopService(serviceIntent)

but none of them stops the service, onDestroy function on the MediaLibraryService was never invoked!!!!why?!!!!

Lightship answered 11/5, 2023 at 22:4 Comment(0)
L
0

The problem was i had two threads running on the service and i didn't invoke release() methods for the session and the player so stopForeground() and stopSelf() didn't work. i tried creating a method to stop and release all and invoked the method on the onStartCommand method with the action "STOP_SERVICE" like the answer here and onDestroy() has been called successfully

Lightship answered 12/5, 2023 at 19:28 Comment(0)
P
0

The stopService() method may not stop the service immediately.

In the case of a foreground service, you need to explicitly call stopForeground(true) within the service's code to remove the service from the foreground and allow it to be stopped. This will trigger the onDestroy() method in your MediaLibraryService.

In your activity:

// Start the service
val serviceIntent = Intent(this@PartyHostActivity, PlayerService::class.java)
startService(serviceIntent)

// Stop the service when the specific button is clicked
specificButton.setOnClickListener {
    val stopIntent = Intent(this@PartyHostActivity, PlayerService::class.java)
    stopIntent.action = "STOP_SERVICE"
    startService(stopIntent)
}

In your MediaLibraryService:

class PlayerService : Service() {
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent?.action == "STOP_SERVICE") {
            // Stop the foreground service and allow it to be stopped
            stopForeground(true)
            stopSelf()
        } else {
            // Start the foreground service
            // Perform other necessary operations
        }
        return START_STICKY
    }
    // override other functions as you wish

}

By sending an intent with the action "STOP_SERVICE", you can differentiate the request to stop the service from other start requests. Within the onStartCommand() method, you check for this action and then call stopForeground(true) and stopSelf() to stop the service properly.

Pesek answered 11/5, 2023 at 22:21 Comment(1)
I'm extending MediaLibraryService not Service and the solution doesn't work!Lightship
L
0

The problem was i had two threads running on the service and i didn't invoke release() methods for the session and the player so stopForeground() and stopSelf() didn't work. i tried creating a method to stop and release all and invoked the method on the onStartCommand method with the action "STOP_SERVICE" like the answer here and onDestroy() has been called successfully

Lightship answered 12/5, 2023 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.