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[ This sample is DEPRECATED. ]]> 33 </intro> 34 </strings> 35 36 <!-- The basic templates have already been enabled. Uncomment more as desired. --> 37 <template src="base-build" /> 38 <template src="base-application" /> 39 40 <metadata> 41 <status>DEPRECATED</status> 42 <categories>Media</categories> 43 <technologies>Android</technologies> 44 <languages>Java</languages> 45 <solutions>Mobile</solutions> 46 <level>ADVANCED</level> 47 <icon>screenshots/icon-web.png</icon> 48 <screenshots> 49 <img>screenshots/1-main.png</img> 50 <img>screenshots/2-notification.png</img> 51 </screenshots> 52 <api_refs> 53 <android>android.support.v4.media.MediaBrowserServiceCompat</android> 54 <android>android.support.v4.media.MediaBrowserCompat</android> 55 <android>android.support.v4.media.session.MediaSessionCompat</android> 56 <android>android.support.v4.media.session.MediaControllerCompat</android> 57 <android>android.support.v4.media.app.NotificationCompat.MediaStyle</android> 58 </api_refs> 59 <description> 60<![CDATA[ 61This sample is deprecated. See the 62[Universal Android Music Player](https://github.com/googlesamples/android-UniversalMusicPlayer) 63sample for a more complete solution for using a MediaSession and MediaBrowserService. 64 65This sample shows how to implement a media app that allows 66background playback of audio, and provide a media library 67that is exposed to other apps. 681. It allows other apps control media playback externally 69using MediaSession. This allows playback to be controlled by 70the Google Assistant, for example. 712. It exposes a simple music library through MediaBrowserService. 72And it provides MediaSession callbacks. This allows it to be used 73by Android Auto, for example. 74When not connected to a car, the app has a very simple UI that 75allows for playback as well as skip to previous and next tracks. 76To learn more about MediaSession and MediaBrowserService, read 77this [article on Medium](https://medium.com/google-developers/understanding-mediasession-part-4-4-dcc77c535f99) 78that 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[ 84To 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 106To 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