1 2 Android MediaBrowserService Sample 3 =================================== 4 5 This sample shows how to implement an audio media app that provides 6 media library metadata and playback controls through a standard 7 service. It exposes a simple music library through the new 8 MediaBrowserService and provides MediaSession callbacks. This allows 9 it to be used in Android Auto, for example. 10 When not connected to a car, the app has a very simple UI that browses 11 the media library and provides simple playback controls. When 12 connected to Android Auto, the same service provides data and callback 13 to the Android Auto UI in the same manner as it provides them to the 14 local UI. 15 16 Introduction 17 ------------ 18 19 To 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 41 To 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 72 Pre-requisites 73 -------------- 74 75 - Android SDK 24 76 - Android Build Tools v24.0.1 77 - Android Support Repository 78 79 Screenshots 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 84 Getting Started 85 --------------- 86 87 This sample uses the Gradle build system. To build this project, use the 88 "gradlew build" command or use "Import Project" in Android Studio. 89 90 Support 91 ------- 92 93 - Google+ Community: https://plus.google.com/communities/105153134372062985968 94 - Stack Overflow: http://stackoverflow.com/questions/tagged/android 95 96 If you've found an error in this sample, please file an issue: 97 https://github.com/googlesamples/android-MediaBrowserService 98 99 Patches are encouraged, and may be submitted by forking this project and 100 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 101 102 License 103 ------- 104 105 Copyright 2016 The Android Open Source Project, Inc. 106 107 Licensed to the Apache Software Foundation (ASF) under one or more contributor 108 license agreements. See the NOTICE file distributed with this work for 109 additional information regarding copyright ownership. The ASF licenses this 110 file to you under the Apache License, Version 2.0 (the "License"); you may not 111 use this file except in compliance with the License. You may obtain a copy of 112 the License at 113 114 http://www.apache.org/licenses/LICENSE-2.0 115 116 Unless required by applicable law or agreed to in writing, software 117 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 118 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 119 License for the specific language governing permissions and limitations under 120 the License. 121