I'm struggling with Android Auto using Desktop Head Unit Emulator. So far I create my media app in Android Auto, start it and all media albums where shown.
However, when I click on an entry, nothing happens. It is not clear to me how to catch the click in order to further start an action.
@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
int maximumRootChildLimit = rootHints.getInt(
MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT,
/* defaultValue= */ 4);
Bundle extras = new Bundle();
extras.putInt(
MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_BROWSABLE,
MediaConstants.DESCRIPTION_EXTRAS_VALUE_CONTENT_STYLE_GRID_ITEM);
extras.putInt(
MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_PLAYABLE,
MediaConstants.DESCRIPTION_EXTRAS_VALUE_CONTENT_STYLE_LIST_ITEM);
extras.putBoolean(
MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, true);
return new BrowserRoot(MY_MEDIA_ROOT_ID, extras);
}
@Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
if (MY_MEDIA_ROOT_ID.equals(parentId)) {
for (Album album : allAlbums) {
.....
MediaDescriptionCompat desc =
new MediaDescriptionCompat.Builder()
.setMediaId(mediaID)
.setTitle(title)
.setSubtitle(artist)
.setIconBitmap(bitmap(mediaID))
.setExtras(extras)
.build();
mediaItems.add(albumList);
}
}
result.sendResult(mediaItems);
}
I set the mediaID in my PlaybackStateCompat.Builder
:
Bundle playbackStateExtras = new Bundle();
playbackStateExtras.putString(
MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID,"mediaID");
playbackstateBuilder.setExtras(playbackStateExtras);
I set the mediaID in my MediaMetadataCompat.Builder
metadataBuilder.putString(MediaMetadata.METADATA_KEY_MEDIA_ID,"mediaID");
mMediaSessionCompat.setMetadata(metadataBuilder.build());
What have I overlooked in the documentation or didn't understand.
Thanks Alejandro
note the following procedure, please:
FLAG_Browsable
: use this flag, then the MediaID is returned in getLoadChildren
.
FLAG_PLAYABLE
: use this flag, then the MediaID is returned to onPlayFromMediaID
. In onPlayFromMediaID
you have to put the logic how your mediaPlayer
knows what to do with the handed over mediaID
I noticed that you didn't define any tabs. In onLoadChildren
several queries on the mediaID
make sense. The first level defines the tabs - these are missing in the post or in your code.
e.g:
MediaDescriptionCompat desc =
new MediaDescriptionCompat.Builder()
.setMediaId(AUDIOBOOK_HEADER)
.setTitle("Library")
.build();
MediaBrowserCompat.MediaItem albumList =
new MediaBrowserCompat.MediaItem(desc,
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
mediaItems.add(albumList);
desc =
new MediaDescriptionCompat.Builder()
.setMediaId(LATEST_HEADER)
.setTitle("Recently played")
.build();
itemList =
new MediaBrowserCompat.MediaItem(desc,
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
mediaItems.add(itemList);