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