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