| 1 |
package ${packageName}; |
| 2 |
|
| 3 |
import android.media.browse.MediaBrowser.MediaItem; |
| 4 |
import android.media.session.MediaSession; |
| 5 |
import android.os.Bundle; |
| 6 |
import android.service.media.MediaBrowserService; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.List; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class provides a MediaBrowser through a service. It exposes the media library to a browsing |
| 13 |
* client, through the onGetRoot and onLoadChildren methods. It also creates a MediaSession and |
| 14 |
* exposes it through its MediaSession.Token, which allows the client to create a MediaController |
| 15 |
* that connects to and send control commands to the MediaSession remotely. This is useful for |
| 16 |
* user interfaces that need to interact with your media session, like Android Auto. You can |
| 17 |
* (should) also use the same service from your app's UI, which gives a seamless playback |
| 18 |
* experience to the user. |
| 19 |
* |
| 20 |
* To implement a MediaBrowserService, you need to: |
| 21 |
* |
| 22 |
* <ul> |
| 23 |
* |
| 24 |
* <li> Extend {@link android.service.media.MediaBrowserService}, implementing the media browsing |
| 25 |
* related methods {@link android.service.media.MediaBrowserService#onGetRoot} and |
| 26 |
* {@link android.service.media.MediaBrowserService#onLoadChildren}; |
| 27 |
* <li> In onCreate, start a new {@link android.media.session.MediaSession} and notify its parent |
| 28 |
* with the session's token {@link android.service.media.MediaBrowserService#setSessionToken}; |
| 29 |
* |
| 30 |
* <li> Set a callback on the |
| 31 |
* {@link android.media.session.MediaSession#setCallback(android.media.session.MediaSession.Callback)}. |
| 32 |
* The callback will receive all the user's actions, like play, pause, etc; |
| 33 |
* |
| 34 |
* <li> Handle all the actual music playing using any method your app prefers (for example, |
| 35 |
* {@link android.media.MediaPlayer}) |
| 36 |
* |
| 37 |
* <li> Update playbackState, "now playing" metadata and queue, using MediaSession proper methods |
| 38 |
* {@link android.media.session.MediaSession#setPlaybackState(android.media.session.PlaybackState)} |
| 39 |
* {@link android.media.session.MediaSession#setMetadata(android.media.MediaMetadata)} and |
| 40 |
* {@link android.media.session.MediaSession#setQueue(java.util.List)}) |
| 41 |
* |
| 42 |
* <li> Declare and export the service in AndroidManifest with an intent receiver for the action |
| 43 |
* android.media.browse.MediaBrowserService |
| 44 |
* |
| 45 |
* </ul> |
| 46 |
* |
| 47 |
* To make your app compatible with Android Auto, you also need to: |
| 48 |
* |
| 49 |
* <ul> |
| 50 |
* |
| 51 |
* <li> Declare a meta-data tag in AndroidManifest.xml linking to a xml resource |
| 52 |
* with a <automotiveApp> root element. For a media app, this must include |
| 53 |
* an <uses name="media"/> element as a child. |
| 54 |
* For example, in AndroidManifest.xml: |
| 55 |
* <meta-data android:name="com.google.android.gms.car.application" |
| 56 |
* android:resource="@xml/automotive_app_desc"/> |
| 57 |
* And in res/values/automotive_app_desc.xml: |
| 58 |
* <automotiveApp> |
| 59 |
* <uses name="media"/> |
| 60 |
* </automotiveApp> |
| 61 |
* |
| 62 |
* </ul> |
| 63 |
|
| 64 |
* @see <a href="README.md">README.md</a> for more details. |
| 65 |
* |
| 66 |
*/ |
| 67 |
public class ${mediaBrowserServiceName} extends MediaBrowserService { |
| 68 |
|
| 69 |
private MediaSession mSession; |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onCreate() { |
| 73 |
super.onCreate(); |
| 74 |
|
| 75 |
mSession = new MediaSession(this, "${mediaBrowserServiceName}"); |
| 76 |
setSessionToken(mSession.getSessionToken()); |
| 77 |
mSession.setCallback(new MediaSessionCallback()); |
| 78 |
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | |
| 79 |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void onDestroy() { |
| 84 |
mSession.release(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) { |
| 89 |
return new BrowserRoot("root", null); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onLoadChildren(final String parentMediaId, final Result<List<MediaItem>> result) { |
| 94 |
result.sendResult(new ArrayList<MediaItem>()); |
| 95 |
} |
| 96 |
|
| 97 |
private final class MediaSessionCallback extends MediaSession.Callback { |
| 98 |
@Override |
| 99 |
public void onPlay() { |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onSkipToQueueItem(long queueId) { |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onSeekTo(long position) { |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onPlayFromMediaId(String mediaId, Bundle extras) { |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onPause() { |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onStop() { |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void onSkipToNext() { |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onSkipToPrevious() { |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void onCustomAction(String action, Bundle extras) { |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void onPlayFromSearch(final String query, final Bundle extras) { |
| 136 |
} |
| 137 |
} |
| 138 |
} |