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.

Pre-requisites

  • 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.

Screenshots

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

Android Wear watch face Android Wear controls

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-UniversalMusicPlayer/issues

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

License

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

http://www.apache.org/licenses/LICENSE-2.0

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 "com.example.android.uamp"

minSdkVersion 17

targetSdkVersion 26

versionCode 2

versionName "1.1"

compile 'com.google.android.gms:play-services-cast-framework:11.0.1'

compile 'com.google.android.support:wearable:2.0.3'

compile 'com.android.support:appcompat-v7:26.1.0'

compile 'com.android.support:cardview-v7:26.1.0'

compile 'com.android.support:mediarouter-v7:26.1.0'

compile 'com.android.support:leanback-v17:26.1.0'

compile 'com.android.support:design:26.1.0'

compile 'com.google.android.exoplayer:exoplayer:r2.5.0'

testCompile 'junit:junit:4.12'

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

package com.example.android.uamp

uses-permission

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

MusicPlayerActivity

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 android.support.v4.media.session.MediaControllerCompat} is connecting.

PlaceholderActivity

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

NowPlayingActivity

The activity for the Now Playing Card PendingIntent. https://developer.android.com/training/tv/playback/now-playing.html * This activity determines which activity to launch based on the current UI mode.

TvBrowseActivity

Main activity for the Android TV user interface.

TvVerticalGridActivity

TvPlaybackActivity

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.

FullScreenPlayerActivity

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.

MusicService

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 android.service.media.MediaBrowserService}, implementing the media browsing related methods {@link android.service.media.MediaBrowserService#onGetRoot} and {@link android.service.media.MediaBrowserService#onLoadChildren};
  • In onCreate, start a new {@link android.media.session.MediaSession} and notify its parent with the session's token {@link android.service.media.MediaBrowserService#setSessionToken}; *
  • Set a callback on the {@link android.media.session.MediaSession#setCallback(android.media.session.MediaSession.Callback)}. 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 android.media.MediaPlayer}) *
  • Update playbackState, "now playing" metadata and queue, using MediaSession proper methods {@link android.media.session.MediaSession#setPlaybackState(android.media.session.PlaybackState)} {@link android.media.session.MediaSession#setMetadata(android.media.MediaMetadata)} and {@link android.media.session.MediaSession#setQueue(java.util.List)}) *
  • Declare and export the service in AndroidManifest with an intent receiver for the action android.media.browse.MediaBrowserService *

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="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc"/> And in res/values/automotive_app_desc.xml: <automotiveApp> <uses name="media"/> </automotiveApp> *

@see README.md for more details. *

(non-Javadoc) @see android.app.Service#onStartCommand(android.content.Intent, int, int)

(non-Javadoc) @see android.app.Service#onDestroy()

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.

MediaButtonReceiver