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