我正在创建一个有声读物播放器,并且正在使用 MediaSessionCompat 相关类来处理通知。我的代码深受 android-MediaBrowserService 示例(https://github.com/googlearchive/android-MediaBrowserService)的启发,我目前还不太了解(尤其是 createContentIntent )
这是我负责从提供元数据和播放状态数据的 bookPlayer 构建通知的简单类
class PlayerNotificationManager(val playerService: PlayerService) {
val CHANNEL_ID = "pylvain.gamma"
val NOTIFICATION_ID = 412
private val REQUEST_CODE = 501
val notificationManager =
playerService.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val playAction: NotificationCompat.Action =
NotificationCompat.Action (
android.R.drawable.ic_media_pause,
"PAUSE",
buildMediaButtonPendingIntent(
playerService,
PlaybackStateCompat.ACTION_PAUSE
)
)
fun getNotification(bookPlayer: BookPlayer): Notification {
if (isAndroidOOrHigher()) createChannel()
val style = androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(playerService.sessionToken)
.setShowCancelButton(true)
.setShowActionsInCompactView(0)
.setCancelButtonIntent(
buildMediaButtonPendingIntent(
playerService,
PlaybackStateCompat.ACTION_STOP
)
)
val builder = NotificationCompat.Builder(playerService, CHANNEL_ID)
.addAction(playAction)
.setStyle(style)
.setSmallIcon(R.drawable.ic_music_rest_quarter)
.setContentIntent(createContentIntent())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
return builder.build()
}
private fun createContentIntent(): PendingIntent { //TODO: Understand that
Timber.i("Creating Intent")
val openUI = Intent(playerService, MainActivity::class.java)
openUI.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
return PendingIntent.getActivity(
playerService, 0, openUI, PendingIntent.FLAG_UPDATE_CURRENT
)
}
通知在元数据中显示得非常好
这是我的 MediaBrowserService 处理媒体会话,我在其中注册了回调。bookplayer 是用 Koin 构建和注入的。:
class PlayerService : MediaBrowserServiceCompat() {
private lateinit var playerNotificationManager: PlayerNotificationManager
lateinit var session: MediaSessionCompat
private val bookPlayer: BookPlayer by inject()
override fun onCreate() {
super.onCreate()
session = MediaSessionCompat(this, "MusicService")
session.apply {
setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
)
setPlaybackState(bookPlayer.playbackState)
setMetadata(bookPlayer.getMetadata())
setCallback(callbacks)
setActive(true)
}
setSessionToken(session.sessionToken)
playerNotificationManager = PlayerNotificationManager(this)
val notification = playerNotificationManager.getNotification(bookPlayer)
startForeground(playerNotificationManager.NOTIFICATION_ID, notification)
}
override fun onTaskRemoved(rootIntent: Intent?) { //TODO
super.onTaskRemoved(rootIntent)
stopSelf()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
override fun onGetRoot(
clientPackageName: String,
clientUid: Int,
rootHints: Bundle?
): BrowserRoot? {
return BrowserRoot("root", null)
}
override fun onLoadChildren(
parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem>>
) {
result.sendResult(null);
}
override fun onDestroy() {
session.release()
}
val callbacks = object : MediaSessionCompat.Callback() {
override fun onCommand(command: String?, extras: Bundle?, cb: ResultReceiver?) {
Timber.i("Test")
super.onCommand(command, extras, cb)
}
override fun onPrepare() {
Timber.i("Preparing")
}
override fun onPlay() {
Timber.i("Playing")
bookPlayer.pause()
}
override fun onPause() {
Timber.i("Pausing")
bookPlayer.pause()
}
override fun onSkipToNext() {}
override fun onSkipToPrevious() {}
override fun onStop() {}
override fun onSeekTo(pos: Long) {}
override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean = true
}
}
然后从主要活动启动服务
startService(Intent(mainContext, PlayerService::class.java))
我还将它添加到我的 Android 清单中
<service android:name=".playerservice.PlayerService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<receiver android:name="androidx.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
每当我按下按钮时,都不会调用任何回调。当我这样做时,应用程序会记录以下文本:
D/MediaBrowserCompat: Connecting to a MediaBrowserService.
什么也没有发生……我搜索了整个互联网,我完全一无所知,但这肯定很简单。有人能帮我吗 ?非常感谢你<3