Courtesy: GitHub

Universal Android Music Player Sample

This sample shows how to implement an audio media app that works across multiple form factors and provide a consistent user experience on Android phones, tablets, Android Auto, Android Wear, Android TV and Google Cast devices.


  • Android SDK v17

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.


Phone Lock screen Full screen player Cast dialog Android Auto Android TV

Android Wear watch face Android Wear controls


If you've found an error in this sample, please file an issue:

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see for more details.


Copyright 2014 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compileSdkVersion 26

applicationId ""

minSdkVersion 17

targetSdkVersion 26

versionCode 2

versionName "1.1"

compile ''

compile ''

compile ''

compile ''

compile ''

compile ''

compile ''

compile ''

testCompile 'junit:junit:4.12'

testCompile 'org.mockito:mockito-core:1.10.19'



  • android.permission.INTERNET
  • android.permission.WAKE_LOCK
  • android.permission.ACCESS_WIFI_STATE
  • android.permission.ACCESS_NETWORK_STATE


Main activity for the music player. This class hold the MediaBrowser and the MediaController instances. It will create a MediaBrowser when it is created and connect/disconnect on start/stop. Thus, a MediaBrowser will be always connected while this activity is running.

Optionally used with {@link #EXTRA_START_FULLSCREEN} to carry a MediaDescription to the {@link FullScreenPlayerActivity}, speeding up the screen rendering while the {@link} is connecting.


Placeholder activity for features that are not implemented in this sample, but are in the navigation drawer.


The activity for the Now Playing Card PendingIntent. * This activity determines which activity to launch based on the current UI mode.


Main activity for the Android TV user interface.



Activity used to display details of the currently playing song, along with playback controls and the playing queue.

Receive callbacks from the MediaController. Here we update our state such as which queue is being shown, the current title and description and the PlaybackState.


A full screen player that shows the current playing music with a background image depicting the album art. The activity also has controls to seek/pause/play the audio.


This class provides a MediaBrowser through a service. It exposes the media library to a browsing client, through the onGetRoot and onLoadChildren methods. It also creates a MediaSession and exposes it through its MediaSession.Token, which allows the client to create a MediaController that connects to and send control commands to the MediaSession remotely. This is useful for user interfaces that need to interact with your media session, like Android Auto. You can (should) also use the same service from your app's UI, which gives a seamless playback experience to the user. To implement a MediaBrowserService, you need to:

  • Extend {@link}, implementing the media browsing related methods {@link} and {@link};
  • In onCreate, start a new {@link} and notify its parent with the session's token {@link}; *
  • Set a callback on the {@link}. The callback will receive all the user's actions, like play, pause, etc; *
  • Handle all the actual music playing using any method your app prefers (for example, {@link}) *
  • Update playbackState, "now playing" metadata and queue, using MediaSession proper methods {@link} {@link} and {@link}) *
  • Declare and export the service in AndroidManifest with an intent receiver for the action *

To make your app compatible with Android Auto, you also need to:

  • Declare a meta-data tag in AndroidManifest.xml linking to a xml resource with a <automotiveApp> root element. For a media app, this must include an <uses name="media"/> element as a child. For example, in AndroidManifest.xml: <meta-data android:name="" android:resource="@xml/automotive_app_desc"/> And in res/values/automotive_app_desc.xml: <automotiveApp> <uses name="media"/> </automotiveApp> *

@see for more details. *

(non-Javadoc) @see, int, int)

(non-Javadoc) @see

Callback method called from PlaybackManager whenever the music is about to play.

Callback method called from PlaybackManager whenever the music stops playing.

A simple handler that stops the service if playback is not active (playing)

Session Manager Listener responsible for switching the Playback instances depending on whether it is connected to a remote player.