代码之家  ›  专栏  ›  技术社区  ›  Simon

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

  •  0
  • Simon  · 技术社区  · 6 年前

    在我的活动中我注册了 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 回复  |  直到 5 年前
        1
  •  8
  •   TheTurboTurnip    6 年前

    使用前 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

    推荐文章