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