5

I'm following the Android guide to use MediaBrowserServiceCompat in a media player app, but the service is destroyed on app exit.

    <service android:name=".media.PlaybackService">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService" />
        </intent-filter>
    </service>

On application start service is created correctly. On button pressed mediaController.transportControls.play() is called and after this onPlay() method from my session callback class is invoked. Inside onPlay() service is started: context.startService(Intent(context, MediaBrowserServiceCompat::class.java)) (also tried startForegroundService for Oreo and higher) and set to foreground: this@PlaybackService.startForeground(SERVICE_ID, notificationBuilder?.build()). Notification channel for Oreo nad higher API is created.

For now notification is launched and I can see service is in foreground by issuing command:

$adb shell dumpsys activity services PlaybackService
(...)
isForeground=true foregroundId=1 foregroundNoti=Notification(channel=MyChannelId pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff000000 category=transport actions=1 vis=PUBLIC)
(...)

So far everything works fine but when I press home button I'm expecting the service remain started, but instead the onDestroy() method of my MediaBrowserServiceCompat class is invoked. After returning to the app and launching service once again it is not set to foreground (no notification as well).

I'm using the only one startService call in my app and removed every call to stopSelf and stopForeground for testing.

On APIs 28, 26, 22 and 18 I'm getting the same result.

I've also tried returning START_STICKY from onStartCommand().

Radek Daniluk
  • 405
  • 6
  • 14
  • 4
    Did you figure out what was the issue? I am running into the exact same problem and it would be great to know what you have done. Google's documentation is just horrible regarding MediaBrowserService and all it's intricacies – Rik van Velzen Dec 08 '19 at 21:16
  • Unfortunately, I've had no time to refine this hobby project, so I've put up with UX degradation. – Radek Daniluk Dec 09 '19 at 17:02
  • @RikvanVelzen Did you figure it out? I am stuck with the same issue, and have not been able to find any relevant documentation – kb_14 Apr 21 '20 at 12:08

1 Answers1

0

It may certainly be that your MediaBrowser connection was the only thing keeping the MediaBrowserService going and closing that connection causes the MediaBrowserService to be destroyed.

In my case, I decided to create the service before the creation of the MediaBrowser:

// Create the media browser service
Intent intent = new Intent(application, MusicService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
} 
else {
    context.startService(mixerIntent);
}

// Create connection with the service
mediaBrowser = MediaBrowserCompat(
        context,
        ComponentName(requireContext(), MusicService::class.java),
        connectionCallbacks,
        null
)

Don't forget: once the service has been created, the service must call its startForeground method within five seconds.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81