0

I have followed the github link

Here is my code

private const val NOTIFICATION_ID = 200
private const val NOTIFICATION_CHANNEL_ID = "notification channel id 1"

class SimpleMediaNotificationManager(val context: Context, val player: Player) {

    @RequiresApi(Build.VERSION_CODES.O)
    fun startNotificationService(
        mediaSessionService: MediaSessionService,
        mediaSession: MediaSession
    ) {
        buildNotification(mediaSession)
        startForegroundNotification(mediaSessionService)
    }

    private fun buildNotification(mediaSession: MediaSession) {
        val notificationManager =
            PlayerNotificationManager.Builder(context, 111, NOTIFICATION_CHANNEL_ID)
                .setChannelImportance(NotificationUtil.IMPORTANCE_LOW)
                .setSmallIconResourceId(R.drawable.music)
                .setChannelDescriptionResourceId(R.string.app_name)
                .setChannelNameResourceId(R.string.app_name)
                .setMediaDescriptionAdapter(audioDescriptor)
                .setNotificationListener(notificationListener)
                .build()


        notificationManager.setPriority(NotificationCompat.PRIORITY_MIN)
        notificationManager.setUseRewindAction(false)
        notificationManager.setUseFastForwardAction(false)
        notificationManager.setUsePreviousAction(true)
        notificationManager.setUsePlayPauseActions(true)
        notificationManager.setUseNextAction(true)
        notificationManager.setPlayer(player)
    }

    private val notificationListener = object : PlayerNotificationManager.NotificationListener {
        override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
            super.onNotificationCancelled(notificationId, dismissedByUser)
//            stopForeground(true)
            if (player?.isPlaying!!) {
                player?.stop()
                player?.release()
            }
        }

        override fun onNotificationPosted(
            notificationId: Int,
            notification: Notification,
            ongoing: Boolean
        ) {
            super.onNotificationPosted(notificationId, notification, ongoing)
//            startForeground(notificationId, notification)
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun startForegroundNotification(mediaSessionService: MediaSessionService) {
        val notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build()
        mediaSessionService.startForeground(NOTIFICATION_ID, notification)
    }

    private val audioDescriptor = object : PlayerNotificationManager.MediaDescriptionAdapter {
        override fun getCurrentContentTitle(player: Player): CharSequence {
            return player.currentMediaItem?.mediaMetadata?.albumTitle!!
        }

        override fun createCurrentContentIntent(player: Player): PendingIntent? {
            return pendingIntent()
        }

        override fun getCurrentContentText(player: Player): CharSequence? {
            return ""
        }

        override fun getCurrentLargeIcon(
            player: Player,
            callback: PlayerNotificationManager.BitmapCallback
        ): Bitmap? {
            val bitmapDrawable: BitmapDrawable =
                ContextCompat.getDrawable(
                    context,
                    R.drawable.cma_logo_render
                ) as BitmapDrawable
            return bitmapDrawable.bitmap
        }
    }

    private fun pendingIntent(): PendingIntent? {
        val intent = Intent(context, MusicPlayerActivity::class.java)
        return PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
    }
}

Player Service class implementing MediaSessionService

class PlayService : MediaSessionService() {

    private var mediaSession: MediaSession? = null
    lateinit var notificationManager: SimpleMediaNotificationManager

    // Create your Player and MediaSession in the onCreate lifecycle event
    override fun onCreate() {
        super.onCreate()
        val player = ExoPlayer.Builder(this).build()
        notificationManager = SimpleMediaNotificationManager(this, player)
        mediaSession =
            MediaSession.Builder(this, player).setSessionActivity(pendingIntent()!!)
                .setId(Random(5).toString())
                .build()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        notificationManager.startNotificationService(
            mediaSessionService = this,
            mediaSession = mediaSession!!
        )
        return super.onStartCommand(intent, flags, startId)
    }

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

    override fun onDestroy() {
        mediaSession?.run {
            player.release()
            release()
            mediaSession = null
        }
        super.onDestroy()
    }

    private fun pendingIntent(): PendingIntent? {
        val intent = Intent(applicationContext, MusicPlayerActivity::class.java)
        return PendingIntent.getActivity(
            applicationContext,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
    }
}

Now in this class

SimpleMediaNotificationManager

despite of using below line, it is not showing next button in notification.

notificationManager.setUseNextAction(true)

Activity class

override fun onStart() {
    super.onStart()
    val sessionToken = SessionToken(this, ComponentName(this, PlayService::class.java))
    val controllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
    controllerFuture.addListener(
        {
            player = controllerFuture.get()
            player?.seekTo(0, 0)
            val mediaMetaData = MediaMetadata.Builder()
                .setAlbumTitle(MusicPlayerData.audioTitle)
                .build()
            val mediaItem =
                MediaItem.Builder().setMediaMetadata(mediaMetaData)
                    .setUri(MusicPlayerData.audioUri)
                    .build()
            player?.setMediaItem(mediaItem)
            player?.prepare()
            player?.play()
            setPlayerControls()
        },
        MoreExecutors.directExecutor()
    )
}

Let me know how to do it. How to display next button in notification?

primo
  • 1,340
  • 3
  • 12
  • 40

0 Answers0