如何修复此 PlayerNotificationManager.createWithNotificationChannel 错误

Muh*_*oor 6 android kotlin android-studio exoplayer exoplayer2.x

在此输入图像描述

我正在开发一个音乐应用程序,但遇到错误,有人可以告诉我如何解决这个问题吗PlayerNotificationManager.createWithNotificationChannel

.createWithNotificationChannel无法识别

class MusicNotificationManager(
    private val context: Context,
    sessionToken: MediaSessionCompat.Token,
    notificationListener: PlayerNotificationManager.NotificationListener,
    private val newSongCallback: () -> Unit
) {
    //creating notification for music player
    private val notificationManager: PlayerNotificationManager

    init {
        val mediaController = MediaControllerCompat(context, sessionToken)
        notificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            NOTIFICATION_CHANNEL_ID,
            R.string.notification_channel_name,
            R.string.notification_channel_description,
            NOTIFICATION_ID,
            DescriptionAdapter(mediaController),
            notificationListener
        ).apply{
            setSmallIcon(R.drawable.ic_music)
            setMediaSessionToken(sessionToken)
        }
    }

    fun showNotification(player: Player){
        notificationManager.setPlayer(player)
    }
    //get current playing song
    private inner class DescriptionAdapter(
        private val mediaController: MediaControllerCompat
    ): PlayerNotificationManager.MediaDescriptionAdapter {
        override fun getCurrentContentTitle(player: Player): CharSequence {
            newSongCallback()
            return mediaController.metadata.description.title.toString()

        }

        override fun createCurrentContentIntent(player: Player): PendingIntent? {
            return mediaController.sessionActivity
        }
        //get current playing song title
        override fun getCurrentContentText(player: Player): CharSequence? {
            return mediaController.metadata.description.subtitle.toString()
        }
        //get current playing song icon
        override fun getCurrentLargeIcon(
            player: Player,
            callback: PlayerNotificationManager.BitmapCallback
        ): Bitmap? {
            Glide.with(context).asBitmap()
                .load(mediaController.metadata.description.iconUri)
                .into(object : CustomTarget<Bitmap>(){
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        callback.onBitmap(resource)
                    }

                    override fun onLoadCleared(placeholder: Drawable?) = Unit
                })
            return null
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试在 build.gradle 文件中添加所有可能的依赖项,但无法解决此问题。

小智 12

PlayerNotificationManager.createWithNotificationChannel()已弃用。以下是实现相同功能的方法:

private val notificationManager: PlayerNotificationManager  

Use Builder 
   init {
        val mediaController = MediaControllerCompat(context, sessionToken)

        notificationManager = PlayerNotificationManager.Builder(
            context,
            NOTIFICATION_ID, CHANNEL_ID)
            .setChannelNameResourceId(R.string.notification_Channel_name)
           .setChannelDescriptionResourceId(R.string.notification_Channel_Description)
            .setMediaDescriptionAdapter(DescriptionAdapter(mediaController))
            .setNotificationListener(notificationListener)
            .build()

    }
Run Code Online (Sandbox Code Playgroud)


Muh*_*oor -3

它的工作原理是将实现从 'com.google.android.exoplayer:exoplayer:2.15.0' 更改为

实现 'com.google.android.exoplayer:exoplayer:2.11.6'