1 2Android MediaBrowserService Sample 3=================================== 4 5This sample shows how to implement a media app that allows 6background playback of audio, and provide a media library 7that is exposed to other apps. 81. It allows other apps control media playback externally 9using MediaSession. This allows playback to be controlled by 10the Google Assistant, for example. 112. It exposes a simple music library through MediaBrowserService. 12And it provides MediaSession callbacks. This allows it to be used 13by Android Auto, for example. 14When not connected to a car, the app has a very simple UI that 15allows for playback as well as skip to previous and next tracks. 16To learn more about MediaSession and MediaBrowserService, read 17this [article on Medium](https://medium.com/google-developers/understanding-mediasession-part-4-4-dcc77c535f99) 18that goes into the architectural details of these APIs. 19 20<img src="screenshots/architecture.png" height="400" alt="Architecture Diagram"/> 21 22Introduction 23------------ 24 25To implement a MediaBrowserService, you need to: 26 27- Extend MediaBrowserServiceCompat, implementing the media 28 browsing related methods onGetRoot and onLoadChildren; 29 30- In onCreate, start a new MediaSession and call super.setSessionToken() with 31 this MediaSession's token; 32 33- Set a MediaSession.Callback class on the MediaSession. The callback class 34 will receive all the user's actions, like play, pause, etc; 35 36- Handle all the actual music playing using any method your app prefers 37 (for example, the Android MediaPlayer class) 38 39- Whenever it changes, update info about the playing item and the playing 40 queue using MediaSession corresponding methods (setMetadata, 41 setPlaybackState, setQueue, setQueueTitle, etc) 42 43- Handle AudioManager focus change events and react appropriately 44 (e.g. pause when audio focus is lost) 45 46 47To make it compatible with Android Auto, you also need to: 48 49- Declare a meta-data tag in AndroidManifest.xml linking to a xml resource 50 with a automotiveApp root element. For a media app, this must include 51 an <uses name="media"/> element as a child. 52 53 For example, in AndroidManifest.xml: 54``` 55 <meta-data android:name="com.google.android.gms.car.application" 56 android:resource="@xml/automotive_app_desc"/> 57``` 58 59 And in res/xml/automotive\_app\_desc.xml: 60``` 61 <?xml version="1.0" encoding="utf-8"?> 62 <automotiveApp> 63 <uses name="media"/> 64 </automotiveApp> 65``` 66 67- Declare and export the service in AndroidManifest.xml: 68``` 69 <service 70 android:name=".service.MusicService" 71 android:exported="true"> 72 <intent-filter> 73 <action android:name="android.media.browse.MediaBrowserService" /> 74 </intent-filter> 75 </service> 76``` 77 78Pre-requisites 79-------------- 80 81- Android SDK 27 82- Android Build Tools v27.0.2 83- Android Support Repository 84 85Screenshots 86------------- 87 88<img src="screenshots/1-main.png" height="400" alt="Screenshot"/> <img src="screenshots/2-notification.png" height="400" alt="Screenshot"/> 89 90Getting Started 91--------------- 92 93This sample uses the Gradle build system. To build this project, use the 94"gradlew build" command or use "Import Project" in Android Studio. 95 96Support 97------- 98 99- Google+ Community: https://plus.google.com/communities/105153134372062985968 100- Stack Overflow: http://stackoverflow.com/questions/tagged/android 101 102If you've found an error in this sample, please file an issue: 103https://github.com/googlesamples/android-MediaBrowserService 104 105Patches are encouraged, and may be submitted by forking this project and 106submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 107 108License 109------- 110 111Copyright 2017 The Android Open Source Project, Inc. 112 113Licensed to the Apache Software Foundation (ASF) under one or more contributor 114license agreements. See the NOTICE file distributed with this work for 115additional information regarding copyright ownership. The ASF licenses this 116file to you under the Apache License, Version 2.0 (the "License"); you may not 117use this file except in compliance with the License. You may obtain a copy of 118the License at 119 120http://www.apache.org/licenses/LICENSE-2.0 121 122Unless required by applicable law or agreed to in writing, software 123distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 124WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 125License for the specific language governing permissions and limitations under 126the License. 127