README.md
1
2 Android MediaBrowserService Sample
3 ===================================
4
5 This sample shows how to implement a media app that allows
6 background playback of audio, and provide a media library
7 that is exposed to other apps.
8 1. It allows other apps control media playback externally
9 using MediaSession. This allows playback to be controlled by
10 the Google Assistant, for example.
11 2. It exposes a simple music library through MediaBrowserService.
12 And it provides MediaSession callbacks. This allows it to be used
13 by Android Auto, for example.
14 When not connected to a car, the app has a very simple UI that
15 allows for playback as well as skip to previous and next tracks.
16 To learn more about MediaSession and MediaBrowserService, read
17 this [article on Medium](https://medium.com/google-developers/understanding-mediasession-part-4-4-dcc77c535f99)
18 that goes into the architectural details of these APIs.
19
20 <img src="screenshots/architecture.png" height="400" alt="Architecture Diagram"/>
21
22 Introduction
23 ------------
24
25 To 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
47 To 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
78 Pre-requisites
79 --------------
80
81 - Android SDK 26
82 - Android Build Tools v26.0.1
83 - Android Support Repository
84
85 Screenshots
86 -------------
87
88 <img src="screenshots/1-main.png" height="400" alt="Screenshot"/> <img src="screenshots/2-notification.png" height="400" alt="Screenshot"/>
89
90 Getting Started
91 ---------------
92
93 This sample uses the Gradle build system. To build this project, use the
94 "gradlew build" command or use "Import Project" in Android Studio.
95
96 Support
97 -------
98
99 - Google+ Community: https://plus.google.com/communities/105153134372062985968
100 - Stack Overflow: http://stackoverflow.com/questions/tagged/android
101
102 If you've found an error in this sample, please file an issue:
103 https://github.com/googlesamples/android-MediaBrowserService
104
105 Patches are encouraged, and may be submitted by forking this project and
106 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
107
108 License
109 -------
110
111 Copyright 2017 The Android Open Source Project, Inc.
112
113 Licensed to the Apache Software Foundation (ASF) under one or more contributor
114 license agreements. See the NOTICE file distributed with this work for
115 additional information regarding copyright ownership. The ASF licenses this
116 file to you under the Apache License, Version 2.0 (the "License"); you may not
117 use this file except in compliance with the License. You may obtain a copy of
118 the License at
119
120 http://www.apache.org/licenses/LICENSE-2.0
121
122 Unless required by applicable law or agreed to in writing, software
123 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
124 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
125 License for the specific language governing permissions and limitations under
126 the License.
127