1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.music 17 18 import android.app.PendingIntent 19 import android.content.Context 20 import android.content.Intent 21 import android.media.browse.MediaBrowser.MediaItem 22 import android.media.session.MediaSession 23 import android.media.session.PlaybackState 24 import android.os.Bundle 25 import android.service.media.MediaBrowserService 26 import android.util.Log 27 28 /** 29 * Provides "background" audio playback capabilities, allowing the 30 * user to switch between activities without stopping playback. 31 */ 32 class MediaPlaybackService : MediaBrowserService() { 33 34 private lateinit var mSession: MediaSession 35 onCreatenull36 override fun onCreate() { 37 super.onCreate() 38 39 // Start a new MediaSession 40 mSession = MediaSession(this, "MediaPlaybackService") 41 // Enable callbacks from MediaButtons and TransportControls 42 mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS 43 or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS) 44 // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player 45 val stateBuilder: PlaybackState.Builder = PlaybackState.Builder().setActions( 46 PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PLAY_PAUSE) 47 mSession.setPlaybackState(stateBuilder.build()) 48 setSessionToken(mSession.getSessionToken()) 49 val context: Context = getApplicationContext() 50 val intent = Intent(context, MusicBrowserActivity::class.java) 51 val pi: PendingIntent = PendingIntent.getActivity( 52 context, 99 /*request code*/, intent, PendingIntent.FLAG_UPDATE_CURRENT) 53 mSession.setSessionActivity(pi) 54 } 55 onStartCommandnull56 override fun onStartCommand(startIntent: Intent?, flags: Int, startId: Int): Int { 57 return START_STICKY 58 } 59 onDestroynull60 override fun onDestroy() {} 61 onGetRootnull62 override fun onGetRoot(clientPackageName: String, clientUid: Int, rootHints: Bundle?): BrowserRoot? { 63 return null 64 } 65 onLoadChildrennull66 override fun onLoadChildren(parentMediaId: String, result: Result<List<MediaItem>>) { 67 result.sendResult(null) 68 } 69 70 private inner class MediaSessionCallback : MediaSession.Callback() { onPlaynull71 override fun onPlay() {} 72 onSkipToQueueItemnull73 override fun onSkipToQueueItem(queueId: Long) {} 74 onSeekTonull75 override fun onSeekTo(position: Long) {} 76 onPlayFromMediaIdnull77 override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {} 78 onPausenull79 override fun onPause() {} 80 onStopnull81 override fun onStop() {} 82 onSkipToNextnull83 override fun onSkipToNext() {} 84 onSkipToPreviousnull85 override fun onSkipToPrevious() {} 86 onPlayFromSearchnull87 override fun onPlayFromSearch(query: String?, extras: Bundle?) {} 88 onCustomActionnull89 override fun onCustomAction(action: String, extras: Bundle?) {} 90 } 91 }