Android Auto isn't automatically binding to MediaBrowserService or MediaLibraryService

600 Views Asked by At

I'm trying to add Android Auto capability to my existing app. It's a messaging app, but some messages have an audio attachment. So far, I was successfully able to create a CarAppService to display the messages themselves, but I can't seem to be able to get the audio playback to connect to the Android Auto (so that it's tied to a a media session and showing playback controls in the Android Auto dashboard).

I followed the instructions here, and I also tried using the new Media3 library (using sample code here), but neither one seems to activate the MediaBrowserService (or MediaLibraryService in case of Media3).

I have the foreground service permission and the browser service declared in the manifest

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<meta-data android:name="com.google.android.gms.car.notification.SmallIcon"
           android:resource="@drawable/ic_auto_icon" />

        <service
            android:name=".auto.MediaService"
            android:foregroundServiceType="mediaPlayback"
            android:exported="true">
            <intent-filter>
                <action android:name="androidx.media3.session.MediaSessionService"/>
                <action android:name="android.media.browse.MediaBrowserService"/>
            </intent-filter>
        </service>

and I have the MediaLibraryService all set with creating a media session, using my existing ExoPlayer instance, and being initialized with the proper MediaLibrarySession.Callback. But the `onCreate()' method of the MediaLibraryService (or MediaBrowserService) never gets called.

What am I missing? Is there something that I need to do in the car app itself to make it bind to the media browser service?

1

There are 1 best solutions below

0
On

I figured out what was happening, and the problem is basically due to almost complete lack of documentation for Android Auto. As I said in my OP, I have two different services set up: CarAppService and MediaBrowserService. Because of that, my app gets shown twice in AA (2 icons that launch 2 completely different versions of the app). So, each service gets treated like a completely separate app. And once I realized that, I created different icons and labels for each service in the Manifest, so at least now I'm able to differentiate them.

When launching the MediaBrowserService, it acts as expected - it automatically binds to the service, and allows the user to browse through the media files (as if it were just a simple media player app).

But even launching the other one (which is the one I was originally posting about), I had to manually create a media browser client, and have it bind to the media browser service using MediaBrowserCompat.

I also figured out a way to have the MediaBrowserService icon hidden, but still retain the functionality to use it for before payback in the background. That can be done by removing the <intent-filter> component of that service from the Manifest. The only problem with doing it that way is that we also lose Android Auto's payback controls, so the media plays without a problem, but it would have to be controlled from the CarAppService, which eliminates the option of controlling it in the background.