3

I have exoplayer integrated within my application based on this link.

I have added a pending intent inside createCurrentContentIntent().

    return PendingIntent.getActivity(
        context, 0,
        Intent(context, MyActivity::class.java), 0
    )

I face an issue over here. I started playing the audio and the player notification also comes up in the status bar. My requirement is to play audio even if the app is in the background. So, I haven't released the player in onStop(). I have added the below code in onDestroy().

    override fun onDestroy() {
        playerNotificationManager?.setPlayer(null)
        player?.stop()
        player?.release()
        player = null
        super.onDestroy()
    }

If I manually kill the application from the background when the player is playing, the notification doesn't go off. So, if I click on the notification it will crash with NullPointerException because MyActivtity is no more.

Could someone suggest a solution for the same?

Anju
  • 9,379
  • 14
  • 55
  • 94
  • When looking at the code, calling setPlayer(/* player= */ null) results in calling notificationManager.cancel(notificationId) in case there was a) a non-null player set and b) a notification has already been started. So I'm pretty sure that works as expected. In case you are running the player in a foreground service, and you've called startForeground(notificationId, notification), the notification would not be removed by the system until you have called stopForeground(/* removeNotification= */ true || false). – Muhammad Ammar May 26 '21 at 19:24

2 Answers2

0

I've implemented ExoPlayer along with MediaSessionCompat and MediaSessionConnector, which allows Exo to manage the media notification (and stuff like audio focus) implicitly.

class MyServiceClass {

    private lateinit var player: SimpleExoPlayer
    private lateinit var playerNotificationManager: PlayerNotificationManager
    private lateinit var mediaSession: MediaSessionCompat
    private lateinit var mediaSessionConnector: MediaSessionConnector

    override fun onCreate() {
        super.onCreate()

        player = ...
        playerNotificationManager = ...
        mediaSession = MediaSessionCompat(this, CONSTANT).apply{ ..setup callback... }
        mediaSessionConnector = MediaSessionConnector(mediaSession)
        mediaSessionConnector.setPlayer(player)
        playerNotificationManager.setMediaSessionToken(mediaSession.token)
        playerNotificationManager.setPlayer(player)

    }

    override fun onDestroy() {

        mediaSession.isActive = false
        mediaSession.release()
        mediaSessionConnector.setPlayer(null)
        playerNotificationManager.setPlayer(null)
        player.release()

        super.onDestroy()
    }
}

This should handle removing the notification, when you kill the app.

I also use a PlayerNotificationReceiver to react to notification changes by the system which is omitted in the code above. Also the whole part of triggering and reacting to notifications in the app is omitted.

sventropy
  • 36
  • 2
0

I changed the complete implementation and used services. This solved the issue.

Anju
  • 9,379
  • 14
  • 55
  • 94