How to fix this PlayerNotificationManager.createWithNotificationChannel error
Asked Answered
U

5

6

enter image description here

I am developing a music app but i am getting error can anybody tell me how to fix this PlayerNotificationManager.createWithNotificationChannel?

.createWithNotificationChannel is not recognizable

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
        }
    }
}

I have tried to add all possible dependencies in my build.gradle file but not able to resolve this issue.

Uhlan answered 23/9, 2021 at 20:27 Comment(2)
Let's see your dependencies in build.gradleMicrofiche
[1]: i.sstatic.net/CEgbi.png [2]: i.sstatic.net/ivxnb.pngUhlan
L
12

PlayerNotificationManager.createWithNotificationChannel() is deprecated. Here is how you can achieve the same functionality:

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()

    }
Lightfingered answered 15/12, 2021 at 16:13 Comment(0)
B
2

Try this:

init {
        val mediaController = MediaControllerCompat(context, sessionToken)
        notificationManager = PlayerNotificationManager.Builder(
            context,
            NOTIFICATION_ID, NOTIFICATION_CHANNEL_ID
        )
            .setChannelNameResourceId(R.string.notification_channel_name)
            .setChannelDescriptionResourceId(R.string.notification_channel_description)
            .setMediaDescriptionAdapter(DescriptionAdapter(mediaController))
            .setNotificationListener(notificationListener)
            .build()
            .apply {
                setSmallIcon(R.drawable.ic_music)
                setMediaSessionToken(sessionToken)
            }
    }
Bookplate answered 23/12, 2022 at 11:11 Comment(0)
H
1

PlayerNotificationManager.createWithNotificationChannel were deprecated in favor of PlayerNotificationManager.Builder

refer

https://github.com/google/ExoPlayer/blob/92e05bcd6608b48cc4d20974f55e6a9136274c33/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java#L717-L719

use

public Builder(Context context,int notificationId,String channelId,MediaDescriptionAdapter mediaDescriptionAdapter)

Hardner answered 29/9, 2021 at 13:3 Comment(0)
O
1

Here is an example.

playerNotificationManager = new PlayerNotificationManager.Builder(this, 151, "1234")
            .setMediaDescriptionAdapter(new PlayerNotificationManager.MediaDescriptionAdapter() {
                @Override
                public CharSequence getCurrentContentTitle(Player player) {
                    return videoTitle;
                }

                @Nullable
                @Override
                public PendingIntent createCurrentContentIntent(Player player) {
                    return null;
                }

                @Nullable
                @Override
                public CharSequence getCurrentContentText(Player player) {
                    return null;
                }

                @Nullable
                @Override
                public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                    if (!VideoPlayerActivity.this.isDestroyed()) {
                        Glide.with(VideoPlayerActivity.this)
                                .asBitmap()
                                .load(new File(mVideoFiles.get(position).getPath()))
                                .into(new CustomTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                                    }
                                    @Override
                                    public void onLoadCleared(@Nullable Drawable placeholder) {

                                    }
                                });

                    }
                    return null;
                }
            }).build();
Oneman answered 7/1, 2023 at 5:41 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bestraddle
U
-3

It works by changing the implementation from 'com.google.android.exoplayer:exoplayer:2.15.0' to

implementation 'com.google.android.exoplayer:exoplayer:2.11.6'

Uhlan answered 25/9, 2021 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.