如何使用Exoplayer的PlayerNotificationManager发送静默前台通知



我使用的是最新版本的Exoplayer(2.16.1(。我使用下面的代码创建前台服务通知,但它带有通知声音。如何防止这种声音?

 override fun onCreate() {
    super.onCreate()
    playerNotificationManager = PlayerNotificationManager
        .Builder(this, 9998, NOTIFICATION_CHANNEL_ID)
        .setMediaDescriptionAdapter(PlayerNotificationAdapter())
        .setChannelImportance(IMPORTANCE_HIGH)
        .setNotificationListener(object : PlayerNotificationManager.NotificationListener {
            override fun onNotificationPosted(
                notificationId: Int,
                notification: Notification,
                ongoing: Boolean
            ) {
                super.onNotificationPosted(notificationId, notification, ongoing)
                if (ongoing) {
                    startForeground(notificationId, notification)
                }
            }
            override fun onNotificationCancelled(
                notificationId: Int,
                dismissedByUser: Boolean
            ) {
                super.onNotificationCancelled(notificationId, dismissedByUser)
                stopSelf()
                stopForeground(false)
            }
        })
        .setChannelNameResourceId(R.string.NotificationChannelName)
        .setChannelDescriptionResourceId(R.string.NotificationChannelDescription)
        .build()
    playerNotificationManager.setSmallIcon(R.drawable.ic_play)
    playerNotificationManager.setPriority(PRIORITY_MAX)
    playerNotificationManager.setPlayer(musicPlayerManager.exoPlayer)
    mediaSessionCompat = MediaSessionCompat(this, MEDIA_SESSION_TAG)
    mediaSessionCompat.isActive = true
    playerNotificationManager.setMediaSessionToken(mediaSessionCompat.sessionToken)
    mediaSessionConnector = MediaSessionConnector(mediaSessionCompat)
    val timelineQueueNavigator = object : TimelineQueueNavigator(mediaSessionCompat) {
        override fun getMediaDescription(
            player: Player,
            windowIndex: Int
        ): MediaDescriptionCompat {
            return MediaDescriptionCompat.Builder().build()
        }
    }
    mediaSessionConnector.setQueueNavigator(timelineQueueNavigator)
    mediaSessionConnector.setPlayer(musicPlayerManager.exoPlayer)
}

请注意,我想使用Exoplayer的PlayerNotificationManager来创建通知。

您可以设置:playerNotificationManager.setPriority(PRIORITY_DEFAULT(在这种情况下进行静默通知

相关内容

  • 没有找到相关文章

最新更新