You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更新ExoPlayer中PlayerNotificationManager的自定义操作

问题:基于PlayerNotificationManager实现通知点赞按钮动态切换图标

我基于简化版UAMP播放器开发,希望在前台通知中实现「添加点赞/取消点赞」的自定义操作,并动态切换对应图标。目前已实现自定义操作及点击响应,但无法动态更新图标;尝试在updateLikeButton()方法中替换CustomActionReceiver,却无法调用notify()更新通知,特此询问通过PlayerNotificationManager是否可实现该需求。

const val ACTION_REMOVE_LIKE = "com.romeat.smashup.media.REMOVE_LIKE_FROM_TRACK"
const val ACTION_ADD_LIKE = "com.romeat.smashup.media.ADD_LIKE_TO_TRACK"

/**
 * A wrapper class for ExoPlayer's PlayerNotificationManager.
 */
internal class SmashupNotificationManager(
    private val context: Context,
    private val sessionToken: MediaSessionCompat.Token,
    notificationListener: PlayerNotificationManager.NotificationListener
) {

    private var notificationManager: PlayerNotificationManager
    private var builder: PlayerNotificationManager.Builder  
    private val platformNotificationManager: NotificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    init {
        val mediaController = MediaControllerCompat(context, sessionToken)

        builder = PlayerNotificationManager.Builder(
            context, 
            NOW_PLAYING_NOTIFICATION_ID, 
            NOW_PLAYING_CHANNEL_ID)
            
        with (builder) {
            setMediaDescriptionAdapter(DescriptionAdapter(mediaController))
            setNotificationListener(notificationListener)
            setChannelNameResourceId(R.string.notification_channel)
            setChannelDescriptionResourceId(R.string.notification_channel_description)

            setCustomActionReceiver(
                object : PlayerNotificationManager.CustomActionReceiver {
                    override fun createCustomActions(
                        context: Context,
                        instanceId: Int
                    ): MutableMap<String, NotificationCompat.Action> {
                        return mutableMapOf(
                            Pair(ACTION_ADD_LIKE, NotificationCompat.Action(
                                R.drawable.ic_baseline_favorite_border_24, "add like",
                                PendingIntent.getBroadcast(
                                    context, 
                                    123, 
                                    Intent(ACTION_ADD_LIKE).setPackage(context.packageName),
                                    PendingIntent.FLAG_IMMUTABLE
                                )
                            )),
                        )
                    }

                    override fun getCustomActions(player: Player): MutableList<String> {
                        return mutableListOf(ACTION_ADD_LIKE)
                    }

                    override fun onCustomAction(player: Player, action: String, intent: Intent) { }
                }
            )
        }
        notificationManager = builder.build()
        notificationManager.setMediaSessionToken(sessionToken)
        notificationManager.setUseRewindAction(false)
        notificationManager.setUseFastForwardAction(false)
    }

    fun updateLikeButton() {
        // Can get here with no problem.
        // Basically I want to replace CustomActionReceiver:
        builder.setCustomActionReceiver(object : PlayerNotificationManager.CustomActionReceiver {
            override fun createCustomActions(
                context: Context,
                instanceId: Int
            ): MutableMap<String, NotificationCompat.Action> {
                return mutableMapOf(
                    Pair(ACTION_REMOVE_LIKE, NotificationCompat.Action(
                        R.drawable.ic_baseline_favorite_24, "remove like",
                        PendingIntent.getBroadcast(
                            context, 
                            123, 
                            Intent(ACTION_REMOVE_LIKE).setPackage(context.packageName), 
                            PendingIntent.FLAG_IMMUTABLE
                        )
                    ))
                )
            }

            override fun getCustomActions(player: Player): MutableList<String> {
                return mutableListOf(ACTION_REMOVE_LIKE)
            }

            override fun onCustomAction(player: Player, action: String, intent: Intent) { }
        })

        ... 
        
        // But I can't use .notify() because second parameter is of PlayerNotificationManager type, not Notification type
        platformNotificationManager.notify(NOW_PLAYING_NOTIFICATION_ID, builder.build())
    }
    
    // other code, like DescriptionAdapter etc...

}

内容的提问来源于stack exchange,提问作者romeat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 17:02:46