56 lines
1.7 KiB
Kotlin
56 lines
1.7 KiB
Kotlin
package com.player.musicoo.service
|
|
|
|
import android.content.Intent
|
|
import androidx.media3.common.Player
|
|
import androidx.media3.common.util.UnstableApi
|
|
import androidx.media3.exoplayer.ExoPlayer
|
|
import androidx.media3.session.MediaSession
|
|
import androidx.media3.session.MediaSessionService
|
|
import com.player.musicoo.R
|
|
|
|
|
|
@UnstableApi
|
|
class LocalPlaybackService : MediaSessionService() {
|
|
|
|
private var mediaSession: MediaSession? = null
|
|
|
|
// Create your player and media session in the onCreate lifecycle event
|
|
override fun onCreate() {
|
|
super.onCreate()
|
|
|
|
val player = ExoPlayer.Builder(this).build()
|
|
mediaSession = MediaSession.Builder(this, player)
|
|
.setId(getString(R.string.app_name) + "LocalPlay")
|
|
.build()
|
|
|
|
|
|
// setMediaNotificationProvider(MyMediaNotificationProvider(this))
|
|
|
|
}
|
|
|
|
// The user dismissed the app from the recent tasks
|
|
override fun onTaskRemoved(rootIntent: Intent?) {
|
|
val player = mediaSession?.player!!
|
|
if (!player.playWhenReady
|
|
|| player.mediaItemCount == 0
|
|
|| player.playbackState == Player.STATE_ENDED
|
|
) {
|
|
// Stop the service if not playing, continue playing in the background
|
|
// otherwise.
|
|
stopSelf()
|
|
}
|
|
}
|
|
|
|
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? =
|
|
mediaSession
|
|
|
|
// Remember to release the player and media session in onDestroy
|
|
override fun onDestroy() {
|
|
mediaSession?.run {
|
|
player.release()
|
|
release()
|
|
mediaSession = null
|
|
}
|
|
super.onDestroy()
|
|
}
|
|
} |