gpt4 book ai didi

android - 来自 URL Exoplayer 的通知图标

转载 作者:行者123 更新时间:2023-11-29 18:44:51 26 4
gpt4 key购买 nike

我正在尝试在使用 exoplayer 的播放器中设置通知图标。我有一个播放列表并且正在使用 concatenatingMediaSource。我有每首歌曲的专辑封面网址,但我不知道如何将其设置为通知图标。我阅读了一些建议使用 AsyncTask 并创建通知 onPostExecute() 的答案,但我不知道如何在 exoplayer playerNotificationManager 中执行此操作。这是我的音频服务类:-

class  AudioPlayerService: Service() {
private var player: SimpleExoPlayer? = null
private var playerNotificationManager: PlayerNotificationManager? = null
private var mediaSession: MediaSessionCompat? = null
private var mediaSessionConnector: MediaSessionConnector? = null

private var songList: ArrayList<MetaData>? = null

private var context: Context? = null

override fun onCreate() {
super.onCreate()

context = this

val descriptionAdapter = object : PlayerNotificationManager.MediaDescriptionAdapter {

override fun getCurrentContentTitle(player: Player?): String {
return songList!![player!!.currentWindowIndex].name
}

override fun getCurrentContentText(player: Player?): String? {
return songList!![player!!.currentWindowIndex].artist
}

override fun getCurrentLargeIcon(player: Player?, callback: PlayerNotificationManager.BitmapCallback?): Bitmap? {
return null
}

override fun createCurrentContentIntent(player: Player?): PendingIntent? {
val intent = Intent(context, MainActivity::class.java)
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
this, "1", R.string.notification_name, 1, descriptionAdapter
)

val notificationListener = object : PlayerNotificationManager.NotificationListener {
override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
startForeground(notificationId, notification)
}

override fun onNotificationCancelled(notificationId: Int) {
stopSelf()
}
}

playerNotificationManager!!.setNotificationListener(
notificationListener
)

mediaSession = MediaSessionCompat(context, "Test")
mediaSession!!.isActive = true
playerNotificationManager!!.setMediaSessionToken(mediaSession!!.sessionToken)

mediaSessionConnector = MediaSessionConnector(mediaSession)

val timelineQueueNavigator = object: TimelineQueueNavigator(mediaSession) {

override fun getMediaDescription(player: Player?, windowIndex: Int): MediaDescriptionCompat {
return getMediaDescription(songList!![windowIndex])
}
}

mediaSessionConnector!!.setQueueNavigator(timelineQueueNavigator)
}


override fun onDestroy() {
super.onDestroy()
mediaSession!!.release()
mediaSessionConnector!!.setPlayer(null, null)
playerNotificationManager!!.setPlayer(null)
player!!.release()
player = null
}

override fun onBind(p0: Intent?): IBinder? {
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
songList = intent!!.getParcelableArrayListExtra<MetaData>("list")

context = this
player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
val dataSource = DefaultDataSourceFactory(
this, Util.getUserAgent(this, getString(R.string.app_name)))
val concatenatingMediaSource = ConcatenatingMediaSource()

for(item in songList!!){
val mediaSource = ExtractorMediaSource.Factory(dataSource)
.createMediaSource(Uri.parse(item.url))
concatenatingMediaSource.addMediaSource(mediaSource)
}

player!!.prepare(concatenatingMediaSource)
player!!.playWhenReady = true

playerNotificationManager!!.setPlayer(player)

mediaSessionConnector!!.setPlayer(player, null)

return START_STICKY
}

private fun getMediaDescription(item: MetaData): MediaDescriptionCompat {
return MediaDescriptionCompat.Builder()
.setMediaId(item.id.toString())
.setTitle(item.name)
.setDescription(item.artist)
.setMediaUri(Uri.parse(item.url))
.build()
}
}

提前致谢。

最佳答案

你可以在覆盖函数 getCurrentLargeIcon 中使用 Glide

override fun getCurrentLargeIcon(
player: Player?,
callback: PlayerNotificationManager.BitmapCallback?
): Bitmap? {
loadBitmap(currentStation?.cover, callback)
return null //or stub image
}

private fun loadBitmap(url: String, callback: PlayerNotificationManager.BitmapCallback?) {
Glide.with(this)
.asBitmap()
.load(url)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
callback?.onBitmap(resource)
}
})
}

关于android - 来自 URL Exoplayer 的通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52210171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com