如何在Media3的PlayerNotificationManager中隐藏直播音频通知进度条?
关于PlayerNotificationManager直播音频通知隐藏进度条的解决方案
是的,完全可以通过自定义通知实现隐藏进度条的需求,以下是具体实现思路和步骤:
自定义通知布局(推荐方案)
默认的Media3通知布局包含进度相关控件,直播场景下不需要,最稳妥的方式是自定义一个不含进度条的通知布局:- 新建XML布局文件(比如
res/layout/custom_live_media_notification.xml),只保留你需要的元素(播放/暂停按钮、直播标题、主播名称等),移除进度条(ProgressBar)和时间显示控件。 - 初始化
PlayerNotificationManager时,通过setCustomContentView和setCustomBigContentView替换默认布局:val playerNotificationManager = PlayerNotificationManager.Builder(context, LIVE_NOTIFICATION_ID, LIVE_CHANNEL_ID) .setMediaDescriptionAdapter(object : PlayerNotificationManager.MediaDescriptionAdapter { override fun getCurrentContentTitle(player: Player): CharSequence { return "你的直播标题" } override fun createCurrentContentIntent(player: Player): PendingIntent? { // 返回直播页面的跳转Intent return null } override fun getCurrentContentText(player: Player): CharSequence? { return "主播名称" } override fun getCurrentLargeIcon(player: Player, callback: PlayerNotificationManager.BitmapCallback): Bitmap? { // 返回直播封面图标 return null } }) .setCustomContentView(RemoteViews(context.packageName, R.layout.custom_live_media_notification)) .setCustomBigContentView(RemoteViews(context.packageName, R.layout.custom_live_media_notification)) .build() - 如需绑定播放/暂停等按钮事件,可通过
RemoteViews的setOnClickPendingIntent方法为自定义布局中的按钮设置点击逻辑。
- 新建XML布局文件(比如
不推荐的替代方式
如果你不想自定义完整布局,尝试通过反射找到默认布局中的进度条控件并设置为GONE,但这种方式风险较高——Media3的默认布局结构可能随版本更新变化,导致代码失效,因此不建议使用。
内容的提问来源于stack exchange,提问作者Bilel
相关产品推荐
相关产品推荐

