Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update default error text in media browser for Android Auto?

I am working on the Android auto media app and I want to set custom error text (or custom background text) when loading items has failed due to a network error etc. By default there are only two edge case scenarios:

  1. "No items" text will be displayed when MediaBrowserServiceCompat's onLoadChildren() method returns an empty list.
  2. Generic "Something went wrong" text with an exclamation mark icon is when an error occurred.

Is there any way I can update one or another? So far I found only one media app that displays a custom message for the second scenario in Android auto. That's Spotify with a message "Spotify is currently set to offline", though the default error icon is the same. I couldn't find any mentioning of the error messages in MediaBrowserServiceCompat documentation and will appreciate any help.

What I've tried so far setting the error state for the media session and returning a null as a result:

@Override
    public void onLoadChildren(@NonNull final String parentId, @NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {
        // some logic ...

        PlaybackStateCompat errorState = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_STOP)
                .setErrorMessage(PlaybackStateCompat.ERROR_CODE_UNKNOWN_ERROR, "MY CUSTOM ERROR TEXT!").build();
        mediaSession.setPlaybackState(errorState);
        // result.sendError(new Bundle()); // <- does nothing
        // result.sendResult(Collections.emptyList()); // default "No items" message will be displayed
        result.sendResult(null);  // <-- this should trigger an error
}

MediaBrowserServiceCompat.Result has a method sendError but it should only be called on custom action (according to the documentation) and there are no examples of how to use it:

https://developer.android.com/reference/androidx/media/MediaBrowserServiceCompat.Result.html#sendError(android.os.Bundle)

like image 862
Vladimir Gladun Avatar asked Aug 02 '19 06:08

Vladimir Gladun


People also ask

What is media browser service?

Media browser services enable applications to browse media content provided by an application and ask the application to start playing it. They may also be used to control content that is already playing by way of a MediaSession .

What is media session in Android?

android.media.session.MediaSession. Allows interaction with media controllers, volume keys, media buttons, and transport controls. A MediaSession should be created when an app wants to publish media playback information or handle media keys.


1 Answers

I found an answer, the MediaSessionCompat's playback state should be updated to the STATE_ERROR. Here is a sample code for showing an error:

void setErrorState(String errorMessage) {
        PlaybackStateCompat playbackState = new PlaybackStateCompat.Builder()
                .setState(PlaybackStateCompat.STATE_ERROR, 0, 0f)
                .setErrorMessage(
                        PlaybackStateCompat.ERROR_CODE_AUTHENTICATION_EXPIRED,
                        errorMessage
                )
                .build();
        mediaSession.setPlaybackState(playbackState);
    }
like image 175
Vladimir Gladun Avatar answered Sep 29 '22 11:09

Vladimir Gladun