• Home
Name Date Size #Lines LOC

..--

.google/03-May-2024-2116

Application/03-May-2024-3,3142,110

gradle/wrapper/03-May-2024-76

screenshots/03-May-2024-

CONTRIB.mdD03-May-20241.6 KiB3627

CONTRIBUTING.mdD03-May-20241.5 KiB3627

LICENSED03-May-202411.1 KiB202169

NOTICED03-May-2024613 1611

README.mdD03-May-20244.1 KiB12089

build.gradleD03-May-202411 120

gradlewD03-May-20245 KiB165122

gradlew.batD03-May-20242.3 KiB9166

packaging.yamlD03-May-2024496 1610

settings.gradleD03-May-202423 31

README.md

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