gpt4 book ai didi

android - 如何修复 'media button receiver could not be found' 异常

转载 作者:行者123 更新时间:2023-12-04 23:46:16 24 4
gpt4 key购买 nike

我正在构建媒体播放器,并希望处理来自“播放”、“暂停”等通知的操作。
在我的 Activity 中我注册了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)
)
)

但是当我按下媒体通知上的操作按钮时,什么也没有发生。
当我添加这些 Action 时,执行
MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)

打印以控制台警告

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

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

最佳答案

使用前MediaButtonReceiver功能,您需要将其添加到 list 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

关于android - 如何修复 'media button receiver could not be found' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52813954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com