[Android] Add support for android auto to start on a random track when shuffle is enabled
Robert Stone
git at videolan.org
Thu Aug 5 06:31:28 UTC 2021
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Tue Jul 27 21:43:27 2021 -0700| [a256140975121eb5122243fcfc0920ed8c50023f] | committer: Nicolas Pomepuy
Add support for android auto to start on a random track when shuffle is enabled
> https://code.videolan.org/videolan/vlc-android/commit/a256140975121eb5122243fcfc0920ed8c50023f
---
.../vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt | 11 ++++++-----
.../vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt | 6 +++---
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 5ed8bfc75..c397995de 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -188,15 +188,15 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
}
MediaSessionBrowser.ID_ARTIST -> {
val tracks = context.getFromMl { getArtist(id)?.tracks }
- if (isActive) tracks?.let { loadMedia(it.toList()) }
+ if (isActive) tracks?.let { loadMedia(it.toList(), allowRandom = true) }
}
MediaSessionBrowser.ID_GENRE -> {
val tracks = context.getFromMl { getGenre(id)?.albums?.flatMap { it.tracks.toList() } }
- if (isActive) tracks?.let { loadMedia(it.toList()) }
+ if (isActive) tracks?.let { loadMedia(it.toList(), allowRandom = true) }
}
MediaSessionBrowser.ID_PLAYLIST -> {
val tracks = context.getFromMl { getPlaylist(id, Settings.includeMissing)?.tracks }
- if (isActive) tracks?.let { loadMedia(it.toList()) }
+ if (isActive) tracks?.let { loadMedia(it.toList(), allowRandom = true) }
}
MediaSessionBrowser.ID_MEDIA -> {
val tracks = context.getFromMl { getMedia(id)?.tracks }
@@ -217,11 +217,12 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
}
}
- private fun loadMedia(mediaList: List<MediaWrapper>?, position: Int = 0) {
+ private fun loadMedia(mediaList: List<MediaWrapper>?, position: Int = 0, allowRandom: Boolean = false) {
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)
+ // Pick a random first track if allowRandom is true and shuffle is enabled
+ playbackService.load(mediaList, if (allowRandom && playbackService.isShuffling) SecureRandom().nextInt(min(mediaList.size, MEDIALIBRARY_PAGE_SIZE)) else position)
}
}
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index a5b8531e2..943e3e14c 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -280,10 +280,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
}
@MainThread
- fun previous(force : Boolean) {
+ fun previous(force: Boolean) {
mediaList.getMedia(currentIndex)?.let { if (it.type == MediaWrapper.TYPE_VIDEO) it.time = player.getCurrentTime() }
- if (hasPrevious() && currentIndex > 0 &&
- ((force || !player.seekable || (player.getCurrentTime() < PREVIOUS_LIMIT_DELAY) || (lastPrevious != -1L && System.currentTimeMillis() - lastPrevious < PREVIOUS_LIMIT_DELAY)))) {
+ if (hasPrevious() &&
+ ((force || !player.seekable || (player.getCurrentTime() < PREVIOUS_LIMIT_DELAY) || (lastPrevious != -1L && System.currentTimeMillis() - lastPrevious < PREVIOUS_LIMIT_DELAY)))) {
val size = mediaList.size()
currentIndex = prevIndex
if (previous.size > 0) previous.pop()
More information about the Android
mailing list