Having Exoplayer controls in both layout and notification bar?

229 Views Asked by At

I'm building a podcast player using the Exoplayer. I've managed to set it up to have the controls both in the layout and the notification bar using exoplayer's PlayerNotificationManager. The problem is, with it tied to the fragment, it stops playing whenever onPause(), onStop() are called, as to be expected.

So I went about making a ForegroundService to run the exoplayer so it would remain playing, which was quite successful, however, the controls in the layout no longer function and only the notification bar controls work.

The service itself is doing what it should, but I'm not sure what I need to do to make the layout controls respond as well.

I'm basically trying to build a standard media player, but I've kinda at a loss for this. The primary focus for exoplayer seems to be video, and I understand its a little heavy compared to mediaplayer. I mainly focused on it because the controls handle beautifully

Does anyone have any advice for how to make exoplayer play nicely in a service with notification bar controls and layout controls?

class PodcastPlayFragment: Fragment(){

private val connection = object : ServiceConnection{ 
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            if (service is PlayPodcastService.PlayPodcastServiceBinder) {
                playerControlView.player = service.getExoPlayer()
            }
        }
}

class PlayPodcastService: Service{
    inner class PlayPodcastServiceBinder: Binder(){
        fun getExoPlayer() = simpleExoPlayer
    }
}

1

There are 1 best solutions below

0
dbarnes On

After a little playing around I realized in onBind() I wasn't returning an IBinder object. Creating an IBinder and returning it on onBind() solved the problem.

private val playPodcastIBinder: IBinder = PlayPodcastServiceBinder()

override fun onBind(intent: Intent?): IBinder? {

        initializePlayer(context)
        return playPodcastIBinder
}