-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
MusicService.kt
671 lines (599 loc) · 26.8 KB
/
MusicService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed 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.
*/
package com.example.android.uamp.media
import android.app.Notification
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.ResultReceiver
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaBrowserCompat.MediaItem
import android.support.v4.media.MediaDescriptionCompat
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.media.MediaBrowserServiceCompat
import androidx.media.MediaBrowserServiceCompat.BrowserRoot.EXTRA_RECENT
import com.example.android.uamp.media.extensions.album
import com.example.android.uamp.media.extensions.flag
import com.example.android.uamp.media.extensions.id
import com.example.android.uamp.media.extensions.toMediaItem
import com.example.android.uamp.media.extensions.trackNumber
import com.example.android.uamp.media.library.AbstractMusicSource
import com.example.android.uamp.media.library.BrowseTree
import com.example.android.uamp.media.library.JsonSource
import com.example.android.uamp.media.library.MEDIA_SEARCH_SUPPORTED
import com.example.android.uamp.media.library.MusicSource
import com.example.android.uamp.media.library.UAMP_BROWSABLE_ROOT
import com.example.android.uamp.media.library.UAMP_EMPTY_ROOT
import com.example.android.uamp.media.library.UAMP_RECENT_ROOT
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.PlaybackException
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.Player.EVENT_MEDIA_ITEM_TRANSITION
import com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED
import com.google.android.exoplayer2.Player.EVENT_POSITION_DISCONTINUITY
import com.google.android.exoplayer2.Player.EVENT_TIMELINE_CHANGED
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.cast.CastPlayer
import com.google.android.exoplayer2.ext.cast.SessionAvailabilityListener
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.ext.mediasession.TimelineQueueNavigator
import com.google.android.exoplayer2.ui.PlayerNotificationManager
import com.google.android.exoplayer2.util.Util
import com.google.android.exoplayer2.util.Util.constrainValue
import com.google.android.gms.cast.framework.CastContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlin.math.max
import kotlin.math.min
/**
* This class is the entry point for browsing and playback commands from the APP's UI
* and other apps that wish to play music via UAMP (for example, Android Auto or
* the Google Assistant).
*
* Browsing begins with the method [MusicService.onGetRoot], and continues in
* the callback [MusicService.onLoadChildren].
*
* For more information on implementing a MediaBrowserService,
* visit [https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice.html](https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice.html).
*
* This class also handles playback for Cast sessions.
* When a Cast session is active, playback commands are passed to a
* [CastPlayer](https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ext/cast/CastPlayer.html),
* otherwise they are passed to an ExoPlayer for local playback.
*/
open class MusicService : MediaBrowserServiceCompat() {
private lateinit var notificationManager: UampNotificationManager
private lateinit var mediaSource: MusicSource
private lateinit var packageValidator: PackageValidator
// The current player will either be an ExoPlayer (for local playback) or a CastPlayer (for
// remote playback through a Cast device).
private lateinit var currentPlayer: Player
private val serviceJob = SupervisorJob()
private val serviceScope = CoroutineScope(Dispatchers.Main + serviceJob)
protected lateinit var mediaSession: MediaSessionCompat
protected lateinit var mediaSessionConnector: MediaSessionConnector
private var currentPlaylistItems: List<MediaMetadataCompat> = emptyList()
private var currentMediaItemIndex: Int = 0
private lateinit var storage: PersistentStorage
/**
* This must be `by lazy` because the source won't initially be ready.
* See [MusicService.onLoadChildren] to see where it's accessed (and first
* constructed).
*/
private val browseTree: BrowseTree by lazy {
BrowseTree(applicationContext, mediaSource)
}
private var isForegroundService = false
private val remoteJsonSource: Uri =
Uri.parse("https://storage.googleapis.com/uamp/catalog.json")
private val uAmpAudioAttributes = AudioAttributes.Builder()
.setContentType(C.CONTENT_TYPE_MUSIC)
.setUsage(C.USAGE_MEDIA)
.build()
private val playerListener = PlayerEventListener()
/**
* Configure ExoPlayer to handle audio focus for us.
* See [Player.AudioComponent.setAudioAttributes] for details.
*/
private val exoPlayer: ExoPlayer by lazy {
SimpleExoPlayer.Builder(this).build().apply {
setAudioAttributes(uAmpAudioAttributes, true)
setHandleAudioBecomingNoisy(true)
addListener(playerListener)
}
}
/**
* If Cast is available, create a CastPlayer to handle communication with a Cast session.
*/
private val castPlayer: CastPlayer? by lazy {
try {
val castContext = CastContext.getSharedInstance(this)
CastPlayer(castContext, CastMediaItemConverter()).apply {
setSessionAvailabilityListener(UampCastSessionAvailabilityListener())
addListener(playerListener)
}
} catch (e : Exception) {
// We wouldn't normally catch the generic `Exception` however
// calling `CastContext.getSharedInstance` can throw various exceptions, all of which
// indicate that Cast is unavailable.
// Related internal bug b/68009560.
Log.i(TAG, "Cast is not available on this device. " +
"Exception thrown when attempting to obtain CastContext. " + e.message)
null
}
}
@ExperimentalCoroutinesApi
override fun onCreate() {
super.onCreate()
// Build a PendingIntent that can be used to launch the UI.
val sessionActivityPendingIntent =
packageManager?.getLaunchIntentForPackage(packageName)?.let { sessionIntent ->
PendingIntent.getActivity(this, 0, sessionIntent, 0)
}
// Create a new MediaSession.
mediaSession = MediaSessionCompat(this, "MusicService")
.apply {
setSessionActivity(sessionActivityPendingIntent)
isActive = true
}
/**
* In order for [MediaBrowserCompat.ConnectionCallback.onConnected] to be called,
* a [MediaSessionCompat.Token] needs to be set on the [MediaBrowserServiceCompat].
*
* It is possible to wait to set the session token, if required for a specific use-case.
* However, the token *must* be set by the time [MediaBrowserServiceCompat.onGetRoot]
* returns, or the connection will fail silently. (The system will not even call
* [MediaBrowserCompat.ConnectionCallback.onConnectionFailed].)
*/
sessionToken = mediaSession.sessionToken
/**
* The notification manager will use our player and media session to decide when to post
* notifications. When notifications are posted or removed our listener will be called, this
* allows us to promote the service to foreground (required so that we're not killed if
* the main UI is not visible).
*/
notificationManager = UampNotificationManager(
this,
mediaSession.sessionToken,
PlayerNotificationListener()
)
// The media library is built from a remote JSON file. We'll create the source here,
// and then use a suspend function to perform the download off the main thread.
mediaSource = JsonSource(source = remoteJsonSource)
serviceScope.launch {
mediaSource.load()
}
// ExoPlayer will manage the MediaSession for us.
mediaSessionConnector = MediaSessionConnector(mediaSession)
mediaSessionConnector.setPlaybackPreparer(UampPlaybackPreparer())
mediaSessionConnector.setQueueNavigator(UampQueueNavigator(mediaSession))
switchToPlayer(
previousPlayer = null,
newPlayer = if (castPlayer?.isCastSessionAvailable == true) castPlayer!! else exoPlayer
)
notificationManager.showNotificationForPlayer(currentPlayer)
packageValidator = PackageValidator(this, R.xml.allowed_media_browser_callers)
storage = PersistentStorage.getInstance(applicationContext)
}
/**
* This is the code that causes UAMP to stop playing when swiping the activity away from
* recents. The choice to do this is app specific. Some apps stop playback, while others allow
* playback to continue and allow users to stop it with the notification.
*/
override fun onTaskRemoved(rootIntent: Intent) {
saveRecentSongToStorage()
super.onTaskRemoved(rootIntent)
/**
* By stopping playback, the player will transition to [Player.STATE_IDLE] triggering
* [Player.EventListener.onPlayerStateChanged] to be called. This will cause the
* notification to be hidden and trigger
* [PlayerNotificationManager.NotificationListener.onNotificationCancelled] to be called.
* The service will then remove itself as a foreground service, and will call
* [stopSelf].
*/
currentPlayer.stop(/* reset= */true)
}
override fun onDestroy() {
mediaSession.run {
isActive = false
release()
}
// Cancel coroutines when the service is going away.
serviceJob.cancel()
// Free ExoPlayer resources.
exoPlayer.removeListener(playerListener)
exoPlayer.release()
}
/**
* Returns the "root" media ID that the client should request to get the list of
* [MediaItem]s to browse/play.
*/
override fun onGetRoot(
clientPackageName: String,
clientUid: Int,
rootHints: Bundle?
): BrowserRoot? {
/*
* By default, all known clients are permitted to search, but only tell unknown callers
* about search if permitted by the [BrowseTree].
*/
val isKnownCaller = packageValidator.isKnownCaller(clientPackageName, clientUid)
val rootExtras = Bundle().apply {
putBoolean(
MEDIA_SEARCH_SUPPORTED,
isKnownCaller || browseTree.searchableByUnknownCaller
)
putBoolean(CONTENT_STYLE_SUPPORTED, true)
putInt(CONTENT_STYLE_BROWSABLE_HINT, CONTENT_STYLE_GRID)
putInt(CONTENT_STYLE_PLAYABLE_HINT, CONTENT_STYLE_LIST)
}
return if (isKnownCaller) {
/**
* By default return the browsable root. Treat the EXTRA_RECENT flag as a special case
* and return the recent root instead.
*/
val isRecentRequest = rootHints?.getBoolean(EXTRA_RECENT) ?: false
val browserRootPath = if (isRecentRequest) UAMP_RECENT_ROOT else UAMP_BROWSABLE_ROOT
BrowserRoot(browserRootPath, rootExtras)
} else {
/**
* Unknown caller. There are two main ways to handle this:
* 1) Return a root without any content, which still allows the connecting client
* to issue commands.
* 2) Return `null`, which will cause the system to disconnect the app.
*
* UAMP takes the first approach for a variety of reasons, but both are valid
* options.
*/
BrowserRoot(UAMP_EMPTY_ROOT, rootExtras)
}
}
/**
* Returns (via the [result] parameter) a list of [MediaItem]s that are child
* items of the provided [parentMediaId]. See [BrowseTree] for more details on
* how this is build/more details about the relationships.
*/
override fun onLoadChildren(
parentMediaId: String,
result: Result<List<MediaItem>>
) {
/**
* If the caller requests the recent root, return the most recently played song.
*/
if (parentMediaId == UAMP_RECENT_ROOT) {
result.sendResult(storage.loadRecentSong()?.let { song -> listOf(song) })
} else {
// If the media source is ready, the results will be set synchronously here.
val resultsSent = mediaSource.whenReady { successfullyInitialized ->
if (successfullyInitialized) {
val children = browseTree[parentMediaId]?.map { item ->
MediaItem(item.description, item.flag)
}
result.sendResult(children)
} else {
mediaSession.sendSessionEvent(NETWORK_FAILURE, null)
result.sendResult(null)
}
}
// If the results are not ready, the service must "detach" the results before
// the method returns. After the source is ready, the lambda above will run,
// and the caller will be notified that the results are ready.
//
// See [MediaItemFragmentViewModel.subscriptionCallback] for how this is passed to the
// UI/displayed in the [RecyclerView].
if (!resultsSent) {
result.detach()
}
}
}
/**
* Returns a list of [MediaItem]s that match the given search query
*/
override fun onSearch(
query: String,
extras: Bundle?,
result: Result<List<MediaItem>>
) {
val resultsSent = mediaSource.whenReady { successfullyInitialized ->
if (successfullyInitialized) {
val resultsList = mediaSource.search(query, extras ?: Bundle.EMPTY)
.map { mediaMetadata ->
MediaItem(mediaMetadata.description, mediaMetadata.flag)
}
result.sendResult(resultsList)
}
}
if (!resultsSent) {
result.detach()
}
}
/**
* Load the supplied list of songs and the song to play into the current player.
*/
private fun preparePlaylist(
metadataList: List<MediaMetadataCompat>,
itemToPlay: MediaMetadataCompat?,
playWhenReady: Boolean,
playbackStartPositionMs: Long
) {
// Since the playlist was probably based on some ordering (such as tracks
// on an album), find which window index to play first so that the song the
// user actually wants to hear plays first.
val initialWindowIndex = if (itemToPlay == null) 0 else metadataList.indexOf(itemToPlay)
currentPlaylistItems = metadataList
currentPlayer.playWhenReady = playWhenReady
currentPlayer.stop()
// Set playlist and prepare.
currentPlayer.setMediaItems(
metadataList.map { it.toMediaItem() }, initialWindowIndex, playbackStartPositionMs)
currentPlayer.prepare()
}
private fun switchToPlayer(previousPlayer: Player?, newPlayer: Player) {
if (previousPlayer == newPlayer) {
return
}
currentPlayer = newPlayer
if (previousPlayer != null) {
val playbackState = previousPlayer.playbackState
if (currentPlaylistItems.isEmpty()) {
// We are joining a playback session. Loading the session from the new player is
// not supported, so we stop playback.
currentPlayer.clearMediaItems()
currentPlayer.stop()
} else if (playbackState != Player.STATE_IDLE && playbackState != Player.STATE_ENDED) {
preparePlaylist(
metadataList = currentPlaylistItems,
itemToPlay = currentPlaylistItems[currentMediaItemIndex],
playWhenReady = previousPlayer.playWhenReady,
playbackStartPositionMs = previousPlayer.currentPosition
)
}
}
mediaSessionConnector.setPlayer(newPlayer)
previousPlayer?.stop(/* reset= */true)
}
private fun saveRecentSongToStorage() {
// Obtain the current song details *before* saving them on a separate thread, otherwise
// the current player may have been unloaded by the time the save routine runs.
if (currentPlaylistItems.isEmpty()) {
return
}
val description = currentPlaylistItems[currentMediaItemIndex].description
val position = currentPlayer.currentPosition
serviceScope.launch {
storage.saveRecentSong(
description,
position
)
}
}
private inner class UampCastSessionAvailabilityListener : SessionAvailabilityListener {
/**
* Called when a Cast session has started and the user wishes to control playback on a
* remote Cast receiver rather than play audio locally.
*/
override fun onCastSessionAvailable() {
switchToPlayer(currentPlayer, castPlayer!!)
}
/**
* Called when a Cast session has ended and the user wishes to control playback locally.
*/
override fun onCastSessionUnavailable() {
switchToPlayer(currentPlayer, exoPlayer)
}
}
private inner class UampQueueNavigator(
mediaSession: MediaSessionCompat
) : TimelineQueueNavigator(mediaSession) {
override fun getMediaDescription(player: Player, windowIndex: Int): MediaDescriptionCompat {
if (windowIndex < currentPlaylistItems.size) {
return currentPlaylistItems[windowIndex].description
}
return MediaDescriptionCompat.Builder().build()
}
}
private inner class UampPlaybackPreparer : MediaSessionConnector.PlaybackPreparer {
/**
* UAMP supports preparing (and playing) from search, as well as media ID, so those
* capabilities are declared here.
*
* TODO: Add support for ACTION_PREPARE and ACTION_PLAY, which mean "prepare/play something".
*/
override fun getSupportedPrepareActions(): Long =
PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID or
PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID or
PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH or
PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
override fun onPrepare(playWhenReady: Boolean) {
val recentSong = storage.loadRecentSong() ?: return
onPrepareFromMediaId(
recentSong.mediaId!!,
playWhenReady,
recentSong.description.extras
)
}
override fun onPrepareFromMediaId(
mediaId: String,
playWhenReady: Boolean,
extras: Bundle?
) {
mediaSource.whenReady {
val itemToPlay: MediaMetadataCompat? = mediaSource.find { item ->
item.id == mediaId
}
if (itemToPlay == null) {
Log.w(TAG, "Content not found: MediaID=$mediaId")
// TODO: Notify caller of the error.
} else {
val playbackStartPositionMs =
extras?.getLong(MEDIA_DESCRIPTION_EXTRAS_START_PLAYBACK_POSITION_MS, C.TIME_UNSET)
?: C.TIME_UNSET
preparePlaylist(
buildPlaylist(itemToPlay),
itemToPlay,
playWhenReady,
playbackStartPositionMs
)
}
}
}
/**
* This method is used by the Google Assistant to respond to requests such as:
* - Play Geisha from Wake Up on UAMP
* - Play electronic music on UAMP
* - Play music on UAMP
*
* For details on how search is handled, see [AbstractMusicSource.search].
*/
override fun onPrepareFromSearch(query: String, playWhenReady: Boolean, extras: Bundle?) {
mediaSource.whenReady {
val metadataList = mediaSource.search(query, extras ?: Bundle.EMPTY)
if (metadataList.isNotEmpty()) {
preparePlaylist(
metadataList,
metadataList[0],
playWhenReady,
playbackStartPositionMs = C.TIME_UNSET
)
}
}
}
override fun onPrepareFromUri(uri: Uri, playWhenReady: Boolean, extras: Bundle?) = Unit
override fun onCommand(
player: Player,
command: String,
extras: Bundle?,
cb: ResultReceiver?
) = false
/**
* Builds a playlist based on a [MediaMetadataCompat].
*
* TODO: Support building a playlist by artist, genre, etc...
*
* @param item Item to base the playlist on.
* @return a [List] of [MediaMetadataCompat] objects representing a playlist.
*/
private fun buildPlaylist(item: MediaMetadataCompat): List<MediaMetadataCompat> =
mediaSource.filter { it.album == item.album }.sortedBy { it.trackNumber }
}
/**
* Listen for notification events.
*/
private inner class PlayerNotificationListener :
PlayerNotificationManager.NotificationListener {
override fun onNotificationPosted(
notificationId: Int,
notification: Notification,
ongoing: Boolean
) {
if (ongoing && !isForegroundService) {
ContextCompat.startForegroundService(
applicationContext,
Intent(applicationContext, this@MusicService.javaClass)
)
startForeground(notificationId, notification)
isForegroundService = true
}
}
override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
stopForeground(true)
isForegroundService = false
stopSelf()
}
}
/**
* Listen for events from ExoPlayer.
*/
private inner class PlayerEventListener : Player.Listener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
when (playbackState) {
Player.STATE_BUFFERING,
Player.STATE_READY -> {
notificationManager.showNotificationForPlayer(currentPlayer)
if (playbackState == Player.STATE_READY) {
// When playing/paused save the current media item in persistent
// storage so that playback can be resumed between device reboots.
// Search for "media resumption" for more information.
saveRecentSongToStorage()
if (!playWhenReady) {
// If playback is paused we remove the foreground state which allows the
// notification to be dismissed. An alternative would be to provide a
// "close" button in the notification which stops playback and clears
// the notification.
stopForeground(false)
isForegroundService = false
}
}
}
else -> {
notificationManager.hideNotification()
}
}
}
override fun onEvents(player: Player, events: Player.Events) {
if (events.contains(EVENT_POSITION_DISCONTINUITY)
|| events.contains(EVENT_MEDIA_ITEM_TRANSITION)
|| events.contains(EVENT_PLAY_WHEN_READY_CHANGED)) {
currentMediaItemIndex = if (currentPlaylistItems.isNotEmpty()) {
constrainValue(
player.currentMediaItemIndex,
/* min= */ 0,
/* max= */ currentPlaylistItems.size - 1
)
} else 0
}
}
override fun onPlayerError(error: PlaybackException) {
var message = R.string.generic_error;
Log.e(TAG, "Player error: " + error.errorCodeName + " (" + error.errorCode + ")");
if (error.errorCode == PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS
|| error.errorCode == PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND) {
message = R.string.error_media_not_found;
}
Toast.makeText(
applicationContext,
message,
Toast.LENGTH_LONG
).show()
}
}
}
/*
* (Media) Session events
*/
const val NETWORK_FAILURE = "com.example.android.uamp.media.session.NETWORK_FAILURE"
/** Content styling constants */
private const val CONTENT_STYLE_BROWSABLE_HINT = "android.media.browse.CONTENT_STYLE_BROWSABLE_HINT"
private const val CONTENT_STYLE_PLAYABLE_HINT = "android.media.browse.CONTENT_STYLE_PLAYABLE_HINT"
private const val CONTENT_STYLE_SUPPORTED = "android.media.browse.CONTENT_STYLE_SUPPORTED"
private const val CONTENT_STYLE_LIST = 1
private const val CONTENT_STYLE_GRID = 2
private const val UAMP_USER_AGENT = "uamp.next"
val MEDIA_DESCRIPTION_EXTRAS_START_PLAYBACK_POSITION_MS = "playback_start_position_ms"
private const val TAG = "MusicService"