【问题标题】:How to modify the auto generated media3 notification?如何修改自动生成的 media3 通知?
【发布时间】:2023-01-24 03:39:44
【问题描述】:

我用 Media3 1.0.0-beta03 构建了一个网络广播播放器。我使用来自的示例代码 Developers page

它会自动生成媒体通知,但我不知道如何为此添加标题和副标题。

这是我的媒体服务:

class PlaybackService : MediaSessionService(), MediaSession.Callback {

    private object LC {
        lateinit var exoPlayer: ExoPlayer
        lateinit var mediaSession: MediaSession
    }

    override fun onCreate() {
        super.onCreate()
        log("----------------------------- MediaSessionService, onCreate")

        LC.exoPlayer = ExoPlayer.Builder(this).build()
        LC.exoPlayer.addListener(ExoListener())
        LC.exoPlayer.setAudioAttributes(AudioAttributes.Builder().setContentType(AUDIO_CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build(),true)

        LC.mediaSession = MediaSession.Builder(this, LC.exoPlayer).setCallback(this).build()
    }

    override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession = LC.mediaSession

    override fun onAddMediaItems(mediaSession: MediaSession, controller: MediaSession.ControllerInfo, mediaItems: MutableList<MediaItem>): ListenableFuture<MutableList<MediaItem>> {
        val updatedMediaItems = mediaItems.map { it.buildUpon().setUri(it.mediaId).build() }.toMutableList()
        return Futures.immediateFuture(updatedMediaItems)
    }

    override fun onDestroy() {
        log("----------------------------- MediaSessionService, onDestroy")
        LC.exoPlayer.stop()
        LC.exoPlayer.release()
        LC.mediaSession.release()
        super.onDestroy()
        exitProcess(0)
    }
}

我试过更新通知

【问题讨论】:

    标签: android kotlin exoplayer android-mediasession android-media3


    【解决方案1】:

    事实证明,它非常简单。

    在 MediaService 类中有一个方法可以覆盖,称为 onUpdateNotification()。它为我们提供媒体会话。

    所以我们可以覆盖它并创建我们自己的 NotificationCompat

    
    // Override this method in your service
    
     override fun onUpdateNotification(session: MediaSession) {
            createNotification(session) //calling method where we create notification
        }
    

    在我们的 createNotification() 方法中,我们创建通知并使用 MediaStyleHelper.MediaStyle() 设置其样式并在那里设置会话参数 如下所示。

    
    and create the notification as always 
    fun  createNotification(session: MediaSession) {
            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(NotificationChannel(notification_id,"Channel", NotificationManager.IMPORTANCE_LOW))
    
            // NotificationCompat here.
            val notificationCompat = NotificationCompat.Builder(this,notification_id)
                // Text can be set here 
                // but I believe setting MediaMetaData to MediaSession would be enough.
                // I havent tested it deeply yet but did display artist from session
                .setSmallIcon(R.drawable.your_drawable)
                .setContentTitle("your Content title")
                .setContentText("your content text")
                // set session here
                .setStyle(MediaStyleNotificationHelper.MediaStyle(session)) 
                .build()
            notificationManager.notify(1,notificationCompat)
        }
    

    我希望这会有所帮助并且还为时不晚。

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多