[Android] Load media in audio-only mode when Android auto is active
Robert Stone
git at videolan.org
Tue Dec 1 13:11:24 CET 2020
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Sun Nov 29 20:30:09 2020 -0800| [036d98886121241f3bb6bdd0ff483a36a0981898] | committer: Nicolas Pomepuy
Load media in audio-only mode when Android auto is active
> https://code.videolan.org/videolan/vlc-android/commit/036d98886121241f3bb6bdd0ff483a36a0981898
---
.../src/org/videolan/vlc/MediaSessionCallback.kt | 30 +++++++++++++---------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index b959a26d1..d6c3045b0 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -121,7 +121,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
mediaId == MediaSessionBrowser.ID_SHUFFLE_ALL -> {
val tracks = context.getFromMl { audio }
if (tracks.isNotEmpty() && isActive) {
- playbackService.load(tracks, Random().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
+ loadMedia(tracks.toList(), Random().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
if (!playbackService.isShuffling) playbackService.shuffle()
} else {
playbackService.displayPlaybackError(R.string.search_no_result)
@@ -131,38 +131,38 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
val tracks = context.getFromMl { recentAudio?.toList() }
if (!tracks.isNullOrEmpty() && isActive) {
val mediaList = tracks.subList(0, tracks.size.coerceAtMost(MediaSessionBrowser.MAX_HISTORY_SIZE))
- playbackService.load(mediaList, 0)
+ loadMedia(mediaList)
}
}
mediaId == MediaSessionBrowser.ID_HISTORY -> {
val tracks = context.getFromMl { lastMediaPlayed()?.toList()?.filter { MediaSessionBrowser.isMediaAudio(it) } }
if (!tracks.isNullOrEmpty() && isActive) {
val mediaList = tracks.subList(0, tracks.size.coerceAtMost(MediaSessionBrowser.MAX_HISTORY_SIZE))
- playbackService.load(mediaList, 0)
+ loadMedia(mediaList)
}
}
mediaId.startsWith(MediaSessionBrowser.ALBUM_PREFIX) -> {
val tracks = context.getFromMl { getAlbum(mediaId.extractId())?.tracks }
- if (isActive) tracks?.let { playbackService.load(it, 0) }
+ if (isActive) tracks?.let { loadMedia(it.toList()) }
}
mediaId.startsWith(MediaSessionBrowser.ARTIST_PREFIX) -> {
val tracks = context.getFromMl { getArtist(mediaId.extractId())?.tracks }
- if (isActive) tracks?.let { playbackService.load(it, 0) }
+ if (isActive) tracks?.let { loadMedia(it.toList()) }
}
mediaId.startsWith(MediaSessionBrowser.GENRE_PREFIX) -> {
val tracks = context.getFromMl { getGenre(mediaId.extractId())?.tracks }
- if (isActive) tracks?.let { playbackService.load(it, 0) }
+ if (isActive) tracks?.let { loadMedia(it.toList()) }
}
mediaId.startsWith(MediaSessionBrowser.PLAYLIST_PREFIX) -> {
val tracks = context.getFromMl { getPlaylist(mediaId.extractId())?.tracks }
- if (isActive) tracks?.let { playbackService.load(it, 0) }
+ if (isActive) tracks?.let { loadMedia(it.toList()) }
}
mediaId.startsWith(ExtensionsManager.EXTENSION_PREFIX) -> {
val id = mediaId.replace(ExtensionsManager.EXTENSION_PREFIX + "_" + mediaId.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1] + "_", "")
onPlayFromUri(id.toUri(), null)
}
else -> try {
- context.getFromMl { getMedia(mediaId.toLong()) }?.let { if (isActive) playbackService.load(it) }
+ context.getFromMl { getMedia(mediaId.toLong()) }?.let { if (isActive) loadMedia(listOf(it)) }
} catch (e: NumberFormatException) {
if (isActive) playbackService.loadLocation(mediaId)
}
@@ -170,6 +170,14 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
}
}
+ private fun loadMedia(mediaList: List<MediaWrapper>?, position: Int = 0) {
+ mediaList?.let { mediaList ->
+ if (AndroidDevices.isCarMode(playbackService.applicationContext))
+ mediaList.forEach { if (it.type == MediaWrapper.TYPE_VIDEO) it.addFlags(MediaWrapper.MEDIA_FORCE_AUDIO) }
+ playbackService.load(mediaList, position)
+ }
+ }
+
private fun String.extractId() = split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1].toLong()
override fun onPlayFromUri(uri: Uri?, extras: Bundle?) = playbackService.loadUri(uri)
@@ -203,7 +211,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
}
if (!isActive) return at launch
if (tracks.isNullOrEmpty() && !items.isNullOrEmpty()) tracks = items[0].tracks
- if (!tracks.isNullOrEmpty()) playbackService.load(tracks, 0)
+ if (!tracks.isNullOrEmpty()) loadMedia(tracks?.toList())
}
}
@@ -221,7 +229,5 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
override fun onRewind() = playbackService.seek((playbackService.time - TEN_SECONDS).coerceAtLeast(0), fromUser = true)
- override fun onSkipToQueueItem(id: Long) {
- playbackService.playIndex(id.toInt())
- }
+ override fun onSkipToQueueItem(id: Long) = playbackService.playIndex(id.toInt())
}
\ No newline at end of file
More information about the Android
mailing list