Home | History | Annotate | Download | only in MediaBrowserService
      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[
     27 This sample shows how to implement an audio media app that provides
     28 media library metadata and playback controls through a standard
     29 service. It exposes a simple music library through the new
     30 MediaBrowserService and provides MediaSession callbacks. This allows
     31 it to be used in Android Auto, for example.
     32 When not connected to a car, the app has a very simple UI that browses
     33 the media library and provides simple playback controls. When
     34 connected to Android Auto, the same service provides data and callback
     35 to the Android Auto UI in the same manner as it provides them to the
     36 local UI.
     37 ]]></intro>
     38     </strings>
     39 
     40     <!-- The basic templates have already been enabled. Uncomment more as desired. -->
     41     <template src="basebuild" />
     42     <metadata>
     43     <status>PUBLISHED</status>
     44     <categories>Media</categories>
     45     <technologies>Android</technologies>
     46     <languages>Java</languages>
     47     <solutions>Mobile</solutions>
     48     <level>ADVANCED</level>
     49     <icon>screenshots/icon-web.png</icon>
     50     <screenshots>
     51         <img>screenshots/1-main.png</img>
     52         <img>screenshots/2-music-play.png</img>
     53         <img>screenshots/3-music-notification.png</img>
     54     </screenshots>
     55     <api_refs>
     56         <android>android.service.media.MediaBrowserService</android>
     57         <android>android.media.browse.MediaBrowser</android>
     58         <android>android.media.session.MediaSession</android>
     59         <android>android.media.session.MediaController</android>
     60         <android>android.app.Notification.MediaStyle</android>
     61     </api_refs>
     62     <description>
     63 <![CDATA[
     64 This sample shows how to implement an audio media app that provides
     65 media library metadata and playback controls through a standard
     66 service. It exposes a simple music library through the new
     67 MediaBrowserService and provides MediaSession callbacks. This allows
     68 it to be used in Android Auto, for example.
     69 When not connected to a car, the app has a very simple UI that browses
     70 the media library and provides simple playback controls. When
     71 connected to Android Auto, the same service provides data and callback
     72 to the Android Auto UI in the same manner as it provides them to the
     73 local UI.
     74 ]]></description>
     75     <intro>
     76 <![CDATA[
     77 To implement a MediaBrowserService, you need to:
     78 
     79 - Extend android.service.media.MediaBrowserService, implementing the media
     80   browsing related methods onGetRoot and onLoadChildren;
     81 
     82 - In onCreate, start a new MediaSession and call super.setSessionToken() with
     83   this MediaSession's token;
     84 
     85 - Set a MediaSession.Callback class on the MediaSession. The callback class
     86   will receive all the user's actions, like play, pause, etc;
     87 
     88 - Handle all the actual music playing using any method your app prefers
     89   (for example, the Android MediaPlayer class)
     90 
     91 - Whenever it changes, update info about the playing item and the playing
     92   queue using MediaSession corresponding methods (setMetadata,
     93   setPlaybackState, setQueue, setQueueTitle, etc)
     94 
     95 - Handle AudioManager focus change events and react appropriately
     96   (e.g. pause when audio focus is lost)
     97 
     98 
     99 To make it compatible with Android Auto, you also need to:
    100 
    101 - Declare a meta-data tag in AndroidManifest.xml linking to a xml resource
    102   with a automotiveApp root element. For a media app, this must include
    103   an &lt;uses name="media"/&gt; element as a child.
    104 
    105   For example, in AndroidManifest.xml:
    106 ```
    107      <meta-data android:name="com.google.android.gms.car.application"
    108        android:resource="@xml/automotive_app_desc"/>
    109 ```
    110 
    111   And in res/xml/automotive\_app\_desc.xml:
    112 ```
    113       <?xml version="1.0" encoding="utf-8"?>
    114       <automotiveApp>
    115           <uses name="media"/>
    116       </automotiveApp>
    117 ```
    118 
    119 - Declare and export the service in AndroidManifest.xml:
    120 ```
    121     <service
    122         android:name=".service.MusicService"
    123         android:exported="true">
    124       <intent-filter>
    125          <action android:name="android.media.browse.MediaBrowserService" />
    126       </intent-filter>
    127     </service>
    128 ```
    129 ]]></intro>
    130     </metadata>
    131 </sample>
    132