Search code examples
androidandroid-automotive

MediaBrowserServiceCompat on Android Automotive stuck in "Loading content..."


I am trying to build a media application for Android Automotive (not to be confused with Android Auto!). It is an application which can be installed directly on car hardware.

Anyways: When I launch the application in the Polestar 2 emulator (or in the car itself), it hangs on the initial screen with a "Loading content..." message.

I have implemented a class extending MediaBrowserServiceCompat, where I am overriding onGetRoot(...) and onLoadChildren(...):

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
    Log.i(TAG, "onGetRoot()");
    return new MediaBrowserServiceCompat.BrowserRoot(MusicLibrary.getRoot(), null);
}

@Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
    Log.i(TAG, "onLoadChildren()");
    result.sendResult(MusicLibrary.getMediaItems());
}

("MusicLibrary" is just this file, which I am using temporarily to try to get things working: https://github.com/googlecodelabs/android-music-player/blob/master/base/mobile/src/main/java/com/example/android/musicplayercodelab/MusicLibrary.java)

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
    android:allowBackup="true"
    android:appCategory="audio"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Test"
    tools:targetApi="31">

    <service
        android:name=".MediaBrowserService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService"/>
        </intent-filter>
    </service>
    
    <receiver android:name="androidx.media.session.MediaButtonReceiver" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

</application>


<uses-feature android:name="android.hardware.type.automotive" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="false" />
<uses-feature android:name="android.hardware.screen.portrait" android:required="false" />
<uses-feature android:name="android.hardware.screen.landscape" android:required="false" />

<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc"/>

compileSdk and targetSdk is 33. minSdk is 29.

Dependencies (perhaps some can be removed):

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.car.app:app:1.2.0'
implementation 'androidx.media:media:1.6.0'

Looking at Logcat, I just get "onCreate()", "onGetRoot()", and "onLoadChildren()". However, nothing is displayed on the screen, except for the "Loading content..." message in the middle of the screen.

Does anybody have a clue about what I am missing here?


Solution

  • After filing a bug with Google, I figured out what was wrong: I was not registering the MediaSessionCompat and registering the associated media session callback in onCreate():

    @Override
    public void onCreate() {
        super.onCreate();
    
        MediaSessionCompat mSession = new MediaSessionCompat(this, "MyMusicService");
        setSessionToken(mSession.getSessionToken());
        mSession.setCallback(new MediaSessionCallback());
    
    }
    

    IMHO, A rather strange reason for this behavior, because it does not have anything to do with displaying the media items. More details can be found here: https://developer.android.com/training/cars/media#registering_mediasession

    Anyways, it also turns out there is a related bug in the Polestar 2 and Volvo emulator images that the emulator GUI will hang in this "Loading Content..." state on any app restarts afterwards. This bug should now have been forwarded to the developer teams.

    The solution is to use Google's own emulator images for the time being...

    Hopefully this will help others. :)