• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3 Copyright 2013 The Android Open Source Project
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16-->
17<sample>
18    <name>MediaBrowserService</name>
19    <group>Media</group>
20    <package>com.example.android.mediabrowserservice</package>
21
22    <minSdk>21</minSdk>
23
24    <strings>
25        <intro>
26            <![CDATA[
27This sample shows how to implement an audio media app that provides
28media library metadata and playback controls through a standard
29service. It exposes a simple music library through the new
30MediaBrowserService and provides MediaSession callbacks. This allows
31it to be used in Android Auto, for example.
32When not connected to a car, the app has a very simple UI that browses
33the media library and provides simple playback controls. When
34connected to Android Auto, the same service provides data and callback
35to the Android Auto UI in the same manner as it provides them to the
36local UI.
37]]></intro>
38    </strings>
39
40    <!-- The basic templates have already been enabled. Uncomment more as desired. -->
41    <template src="base-build" />
42    <template src="base-application" />
43    <metadata>
44    <status>PUBLISHED</status>
45    <categories>Media</categories>
46    <technologies>Android</technologies>
47    <languages>Java</languages>
48    <solutions>Mobile</solutions>
49    <level>ADVANCED</level>
50    <icon>screenshots/icon-web.png</icon>
51    <screenshots>
52        <img>screenshots/1-main.png</img>
53        <img>screenshots/2-music-play.png</img>
54        <img>screenshots/3-music-notification.png</img>
55    </screenshots>
56    <api_refs>
57        <android>android.service.media.MediaBrowserService</android>
58        <android>android.media.browse.MediaBrowser</android>
59        <android>android.media.session.MediaSession</android>
60        <android>android.media.session.MediaController</android>
61        <android>android.app.Notification.MediaStyle</android>
62    </api_refs>
63    <description>
64<![CDATA[
65This sample shows how to implement an audio media app that provides
66media library metadata and playback controls through a standard
67service. It exposes a simple music library through the new
68MediaBrowserService and provides MediaSession callbacks. This allows
69it to be used in Android Auto, for example.
70When not connected to a car, the app has a very simple UI that browses
71the media library and provides simple playback controls. When
72connected to Android Auto, the same service provides data and callback
73to the Android Auto UI in the same manner as it provides them to the
74local UI.
75]]></description>
76    <intro>
77<![CDATA[
78To implement a MediaBrowserService, you need to:
79
80- Extend android.service.media.MediaBrowserService, implementing the media
81  browsing related methods onGetRoot and onLoadChildren;
82
83- In onCreate, start a new MediaSession and call super.setSessionToken() with
84  this MediaSession's token;
85
86- Set a MediaSession.Callback class on the MediaSession. The callback class
87  will receive all the user's actions, like play, pause, etc;
88
89- Handle all the actual music playing using any method your app prefers
90  (for example, the Android MediaPlayer class)
91
92- Whenever it changes, update info about the playing item and the playing
93  queue using MediaSession corresponding methods (setMetadata,
94  setPlaybackState, setQueue, setQueueTitle, etc)
95
96- Handle AudioManager focus change events and react appropriately
97  (e.g. pause when audio focus is lost)
98
99
100To make it compatible with Android Auto, you also need to:
101
102- Declare a meta-data tag in AndroidManifest.xml linking to a xml resource
103  with a automotiveApp root element. For a media app, this must include
104  an &lt;uses name="media"/&gt; element as a child.
105
106  For example, in AndroidManifest.xml:
107```
108     <meta-data android:name="com.google.android.gms.car.application"
109       android:resource="@xml/automotive_app_desc"/>
110```
111
112  And in res/xml/automotive\_app\_desc.xml:
113```
114      <?xml version="1.0" encoding="utf-8"?>
115      <automotiveApp>
116          <uses name="media"/>
117      </automotiveApp>
118```
119
120- Declare and export the service in AndroidManifest.xml:
121```
122    <service
123        android:name=".service.MusicService"
124        android:exported="true">
125      <intent-filter>
126         <action android:name="android.media.browse.MediaBrowserService" />
127      </intent-filter>
128    </service>
129```
130]]></intro>
131    </metadata>
132</sample>
133