簡體   English   中英

如何使用 Exoplayer 的 PlayerNotificationManager 發送靜默前台通知

[英]How to send silent foreground notification using Exoplayer's 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) 在這種情況下發出靜默通知

0

在通知管理器中發布和取消通知時,我看不到名稱 stopSelf 和 startForeground 的任何樂趣,而不是這個,我得到 startForegroundservice ()。

請分享 startForeground() 和 stopSelf() 的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM