Media3 MediaSessionService如何移除通知栏中的进度条?
移除Media3 MediaSessionService通知栏进度条的方法
针对流式音频(无固定时长)场景,要移除通知栏的进度条,有两种可靠实现方式:
方法一:通过MediaMetadata标记媒体为无固定时长
这是最简洁的方案:在构建MediaMetadata时设置时长为-1L,Media3的通知系统会自动识别并隐藏进度条。
修改你的MediaMetadata构建代码:
val builder = MediaMetadata.Builder().apply { setStation(args.name) setDisplayTitle(args.name) setSubtitle(getString(R.string.radio_notification_description, args.name)) setArtworkUri(args.logoUrl?.toUri()) setIsBrowsable(false) // **关键**:设置时长为-1,标记为无固定时长的流式媒体 setDuration(-1L) } val media = MediaItem.Builder().setMediaId(url).setMediaMetadata(builder.build()).build() controller.setMediaItem(media) controller.prepare() controller.play()
原理:Media3默认通知逻辑会检查媒体时长,当时长为-1时,判定为不可拖动的流式内容,自动隐藏进度条。
方法二:自定义MediaNotificationProvider强制隐藏进度条
如果方法一不生效,或需要更精细的通知控制,可以在自定义Provider中修改默认通知的MediaStyle配置:
@UnstableApi class CustomMediaNotificationProvider(private val context: Context) : MediaNotification.Provider { override fun createNotification( mediaSession: MediaSession, customLayout: ImmutableList<CommandButton>, actionFactory: MediaNotification.ActionFactory, onNotificationChangedCallback: MediaNotification.Provider.Callback ): MediaNotification { // 获取默认通知实例 val defaultProvider = DefaultMediaNotificationProvider.Builder(context).build() val defaultNotification = defaultProvider.createNotification(mediaSession, customLayout, actionFactory, onNotificationChangedCallback) // 重新构建通知,强制隐藏进度条 val modifiedNotification = NotificationCompat.Builder(context, defaultNotification.notification.channelId) .setContentIntent(defaultNotification.notification.contentIntent) .setSmallIcon(R.drawable.ic_notification) // 替换为你的通知小图标 .setStyle(NotificationCompat.MediaStyle() .setMediaSession(mediaSession.sessionToken) .setShowActionsInCompactView(0,1) // 按需保留紧凑视图的操作按钮 .setShowProgressBar(false)) // **关键**:强制隐藏进度条 .setContentTitle(defaultNotification.notification.extras.getCharSequence(Notification.EXTRA_TITLE)) .setContentText(defaultNotification.notification.extras.getCharSequence(Notification.EXTRA_TEXT)) .setLargeIcon(defaultNotification.notification.largeIcon) .setOngoing(true) .build() return MediaNotification(defaultNotification.notificationId, modifiedNotification) } override fun handleCustomCommand( session: MediaSession, action: String, extras: Bundle ): Boolean { return false // 无自定义命令时返回false即可 } }
记得在MediaSessionService中注册这个自定义Provider:
override fun onCreate() { super.onCreate() val mediaSession = MediaSession.Builder(this, player).build() setMediaSession(mediaSession) // 注册自定义通知Provider mediaSession.setNotificationProvider(CustomMediaNotificationProvider(this)) }
补充:动态控制进度条(重写onUpdateNotification)
如果需要在通知更新时动态调整进度条显示,可以重写MediaSessionService的onUpdateNotification方法:
@RequiresApi(Build.VERSION_CODES.O) override fun onUpdateNotification(session: MediaSession, startInForegroundRequired: Boolean) { val currentNotification = session.notificationProvider?.createNotification(session, ImmutableList.of(), DefaultMediaNotificationProvider.ActionFactory(this), object : MediaNotification.Provider.Callback { override fun onNotificationChanged(notification: MediaNotification) { updateNotification(notification) } }) currentNotification?.let { val modifiedNotification = NotificationCompat.Builder(this, it.notification.channelId) .setStyle(NotificationCompat.MediaStyle().setShowProgressBar(false)) .apply { // 复制原通知的核心属性 setContentIntent(it.notification.contentIntent) setSmallIcon(it.notification.smallIcon) setContentTitle(it.notification.extras.getCharSequence(Notification.EXTRA_TITLE)) setContentText(it.notification.extras.getCharSequence(Notification.EXTRA_TEXT)) setLargeIcon(it.notification.largeIcon) setOngoing(true) } .build() updateNotification(MediaNotification(it.notificationId, modifiedNotification)) } }
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

