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