首页 文章

Hanlde NotificationCompat活动中的动作(音频播放器)

提问于
浏览
0

我正在构建媒体播放器,并希望处理来自“播放”,“暂停”等通知的操作 . 在我的活动中,我注册了MediaButtonReceiver

registerReceiver(MediaButtonReceiver(), IntentFilter(Intent.ACTION_MEDIA_BUTTON))

我用它创建媒体通知

val builder: NotificationCompat.Builder = MediaStyleHelper.from(this, mediaSession!!)
builder.addAction(
                NotificationCompat.Action(
                        android.R.drawable.ic_media_previous,
                        "Previous",
                        MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                )
        )

但是当我在媒体通知上按动作按钮时,没有任何反应 . 当我添加这些动作时,执行

MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)

打印以控制警告

“W / MediaButtonReceiver:在给定的上下文中找不到唯一的媒体按钮接收器,因此无法构建待处理的意图 . ”

但是,如果我以编程方式注册它,为什么不注册?

1 回答

  • 0

    在使用 MediaButtonReceiver 函数之前,需要将其添加到清单as described in the docs:

    <receiver android:name="androidx.media.session.MediaButtonReceiver" >
      <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
      </intent-filter>
    </receiver>
    

    如果您还没有使用AndroidX库,则需要使用旧的类名: android.support.v4.media.session.MediaButtonReceiver

相关问题