我使用 MediaBrowserServiceCompat 播放音频。面临无法从当前播放列表中删除项目的问题。没有错误,没有警告。告诉我可能是什么问题?
class MusicServiceConnection(context: Context, serviceComponent: ComponentName) {
//...
fun removeItem(description: MediaDescriptionCompat) {
mediaController.removeQueueItem(description)
}
}
class MediaPlaybackService : MediaBrowserServiceCompat() {
//...
override fun onCreate() {
super.onCreate()
val sessionActivityPendingIntent =
packageManager?.getLaunchIntentForPackage(packageName)?.let { sessionIntent ->
PendingIntent.getActivity(this, 0, sessionIntent, 0)
}
val mediaSession = MediaSessionCompat(this, MediaPlaybackService::class.java.simpleName).apply {
setSessionActivity(sessionActivityPendingIntent)
isActive = true
}
sessionToken = mediaSession.sessionToken
notificationManager = MediaNotificationManager(this, mediaSession.sessionToken, PlayerNotificationListener())
// Load tracks
serviceScope.launch {
mediaStorage.load()
}
mediaSessionConnector = MediaSessionConnector(mediaSession).also {
it.mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS)
}
mediaSessionConnector.setPlaybackPreparer(MediaPlaybackPreparer(mediaStorage,::preparePlaylist) { currentPlaylistItems }
mediaSessionConnector.setQueueNavigator(MediaQueueNavigator(mediaSession))
}
}