This is a great use case in addition to Marc Bächinger’s Playback Notifications with ExoPlayer. Coinverse uses the PlayerNotificationManager
outlined in Marc’s post in order to display a notification player for audiocasts. Then, the pattern above is implemented to return a SimpleExoPlayer instance that displays the player in the main view when the app is opened.
For Android 9.0 it’s important to refactor startService(...)
with ContextCompat.startForegroundService(...)
. Otherwise, if a service is attempted to be stopped when no Foreground Services are running, there will be a Null Pointer Exception crash.