package ${packageName};
import android.media.browse.MediaBrowser.MediaItem;
import android.media.session.MediaSession;
import android.os.Bundle;
import android.service.media.MediaBrowserService;
import java.util.ArrayList;
import java.util.List;
/**
* This class provides a MediaBrowser through a service. It exposes the media library to a browsing
* client, through the onGetRoot and onLoadChildren methods. It also creates a MediaSession and
* exposes it through its MediaSession.Token, which allows the client to create a MediaController
* that connects to and send control commands to the MediaSession remotely. This is useful for
* user interfaces that need to interact with your media session, like Android Auto. You can
* (should) also use the same service from your app's UI, which gives a seamless playback
* experience to the user.
*
* To implement a MediaBrowserService, you need to:
*
*
*
* - Extend {@link android.service.media.MediaBrowserService}, implementing the media browsing
* related methods {@link android.service.media.MediaBrowserService#onGetRoot} and
* {@link android.service.media.MediaBrowserService#onLoadChildren};
*
- In onCreate, start a new {@link android.media.session.MediaSession} and notify its parent
* with the session's token {@link android.service.media.MediaBrowserService#setSessionToken};
*
*
- Set a callback on the
* {@link android.media.session.MediaSession#setCallback(android.media.session.MediaSession.Callback)}.
* The callback will receive all the user's actions, like play, pause, etc;
*
*
- Handle all the actual music playing using any method your app prefers (for example,
* {@link android.media.MediaPlayer})
*
*
- Update playbackState, "now playing" metadata and queue, using MediaSession proper methods
* {@link android.media.session.MediaSession#setPlaybackState(android.media.session.PlaybackState)}
* {@link android.media.session.MediaSession#setMetadata(android.media.MediaMetadata)} and
* {@link android.media.session.MediaSession#setQueue(java.util.List)})
*
*
- Declare and export the service in AndroidManifest with an intent receiver for the action
* android.media.browse.MediaBrowserService
*
*
*
* To make your app compatible with Android Auto, you also need to:
*
*
*
* - Declare a meta-data tag in AndroidManifest.xml linking to a xml resource
* with a <automotiveApp> root element. For a media app, this must include
* an <uses name="media"/> element as a child.
* For example, in AndroidManifest.xml:
* <meta-data android:name="com.google.android.gms.car.application"
* android:resource="@xml/automotive_app_desc"/>
* And in res/values/automotive_app_desc.xml:
* <automotiveApp>
* <uses name="media"/>
* </automotiveApp>
*
*
* @see README.md for more details.
*
*/
public class ${mediaBrowserServiceName} extends MediaBrowserService {
private MediaSession mSession;
@Override
public void onCreate() {
super.onCreate();
mSession = new MediaSession(this, "${mediaBrowserServiceName}");
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
}
@Override
public void onDestroy() {
mSession.release();
}
@Override
public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
return new BrowserRoot("root", null);
}
@Override
public void onLoadChildren(final String parentMediaId, final Result> result) {
result.sendResult(new ArrayList());
}
private final class MediaSessionCallback extends MediaSession.Callback {
@Override
public void onPlay() {
}
@Override
public void onSkipToQueueItem(long queueId) {
}
@Override
public void onSeekTo(long position) {
}
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
}
@Override
public void onPause() {
}
@Override
public void onStop() {
}
@Override
public void onSkipToNext() {
}
@Override
public void onSkipToPrevious() {
}
@Override
public void onCustomAction(String action, Bundle extras) {
}
@Override
public void onPlayFromSearch(final String query, final Bundle extras) {
}
}
}