Commit 69fd77c1 authored by Robert Stone's avatar Robert Stone
Browse files

Add support for android auto to start on a random track when shuffle is enabled

parent 88a86a5a
Pipeline #120049 passed with stage
in 2 minutes and 40 seconds
...@@ -188,15 +188,15 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService ...@@ -188,15 +188,15 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
} }
MediaSessionBrowser.ID_ARTIST -> { MediaSessionBrowser.ID_ARTIST -> {
val tracks = context.getFromMl { getArtist(id)?.tracks } 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 -> { MediaSessionBrowser.ID_GENRE -> {
val tracks = context.getFromMl { getGenre(id)?.albums?.flatMap { it.tracks.toList() } } 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 -> { MediaSessionBrowser.ID_PLAYLIST -> {
val tracks = context.getFromMl { getPlaylist(id, Settings.includeMissing)?.tracks } 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 -> { MediaSessionBrowser.ID_MEDIA -> {
val tracks = context.getFromMl { getMedia(id)?.tracks } val tracks = context.getFromMl { getMedia(id)?.tracks }
...@@ -217,11 +217,12 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService ...@@ -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 -> mediaList?.let { mediaList ->
if (AndroidDevices.isCarMode(playbackService.applicationContext)) if (AndroidDevices.isCarMode(playbackService.applicationContext))
mediaList.forEach { if (it.type == MediaWrapper.TYPE_VIDEO) it.addFlags(MediaWrapper.MEDIA_FORCE_AUDIO) } 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)
} }
} }
......
...@@ -281,7 +281,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList ...@@ -281,7 +281,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
@MainThread @MainThread
fun previous(force : Boolean) { fun previous(force : Boolean) {
mediaList.getMedia(currentIndex)?.let { if (it.type == MediaWrapper.TYPE_VIDEO) it.time = player.getCurrentTime() } mediaList.getMedia(currentIndex)?.let { if (it.type == MediaWrapper.TYPE_VIDEO) it.time = player.getCurrentTime() }
if (hasPrevious() && currentIndex > 0 && if (hasPrevious() && currentIndex != -1 &&
((force || !player.seekable || (player.getCurrentTime() < PREVIOUS_LIMIT_DELAY) || (lastPrevious != -1L && System.currentTimeMillis() - lastPrevious < PREVIOUS_LIMIT_DELAY)))) { ((force || !player.seekable || (player.getCurrentTime() < PREVIOUS_LIMIT_DELAY) || (lastPrevious != -1L && System.currentTimeMillis() - lastPrevious < PREVIOUS_LIMIT_DELAY)))) {
val size = mediaList.size() val size = mediaList.size()
currentIndex = prevIndex currentIndex = prevIndex
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment