From 8c160a31ebb3c390842c5459cd8b394409436839 Mon Sep 17 00:00:00 2001 From: tzugen Date: Tue, 5 Jul 2022 22:51:37 +0200 Subject: [PATCH] Save shuffel/repeat mode in PlaybackState --- .../moire/ultrasonic/service/PlaybackState.kt | 26 ++++++------- .../ultrasonic/fragment/BookmarksFragment.kt | 11 ++++-- .../service/MediaPlayerController.kt | 31 ++++++++------- .../service/MediaPlayerLifecycleSupport.kt | 8 ++-- .../service/PlaybackStateSerializer.kt | 39 ++++++++++++------- 5 files changed, 67 insertions(+), 48 deletions(-) diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/PlaybackState.kt b/ultrasonic/src/main/java/org/moire/ultrasonic/service/PlaybackState.kt index f3e50046..a40e7b05 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/PlaybackState.kt +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/PlaybackState.kt @@ -1,19 +1,19 @@ -package org.moire.ultrasonic.service; +package org.moire.ultrasonic.service -import org.moire.ultrasonic.domain.Track; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import java.io.Serializable +import org.moire.ultrasonic.domain.Track /** * Represents the state of the Media Player implementation */ -public class State implements Serializable -{ - public static final long serialVersionUID = -6346438781062572270L; - - public List songs = new ArrayList<>(); - public int currentPlayingIndex; - public int currentPlayingPosition; +data class PlaybackState( + val songs: List = listOf(), + val currentPlayingIndex: Int = 0, + val currentPlayingPosition: Int = 0, + var shufflePlay: Boolean = false, + var repeatMode: Int = 0 +) : Serializable { + companion object { + const val serialVersionUID = -293487987L + } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/BookmarksFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/BookmarksFragment.kt index 92fe2a52..25aa3b98 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/BookmarksFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/BookmarksFragment.kt @@ -17,6 +17,7 @@ import org.moire.ultrasonic.adapters.BaseAdapter import org.moire.ultrasonic.domain.MusicDirectory import org.moire.ultrasonic.domain.Track import org.moire.ultrasonic.fragment.FragmentTitle.Companion.setTitle +import org.moire.ultrasonic.service.PlaybackState /** * Lists the Bookmarks available on the server @@ -65,12 +66,14 @@ class BookmarksFragment : TrackCollectionFragment() { private fun playNow(songs: List) { if (songs.isNotEmpty()) { - val position = songs[0].bookmarkPosition - - mediaPlayerController.restore( + val state = PlaybackState( songs = songs, currentPlayingIndex = 0, - currentPlayingPosition = position, + currentPlayingPosition = songs[0].bookmarkPosition + ) + + mediaPlayerController.restore( + state = state, autoPlay = true, newPlaylist = true ) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt index 40b2ea26..e43417c5 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -256,9 +256,7 @@ class MediaPlayerController( @Synchronized fun restore( - songs: List, - currentPlayingIndex: Int, - currentPlayingPosition: Int, + state: PlaybackState, autoPlay: Boolean, newPlaylist: Boolean ) { @@ -266,21 +264,24 @@ class MediaPlayerController( else InsertionMode.APPEND addToPlaylist( - songs, + state.songs, cachePermanently = false, autoPlay = false, shuffle = false, insertionMode = insertionMode ) - if (currentPlayingIndex != -1) { + repeatMode = state.repeatMode + isShufflePlayEnabled = state.shufflePlay + + if (state.currentPlayingIndex != -1) { if (jukeboxMediaPlayer.isEnabled) { jukeboxMediaPlayer.skip( - currentPlayingIndex, - currentPlayingPosition / 1000 + state.currentPlayingIndex, + state.currentPlayingPosition / 1000 ) } else { - seekTo(currentPlayingIndex, currentPlayingPosition) + seekTo(state.currentPlayingIndex, state.currentPlayingPosition) } prepare() @@ -445,8 +446,8 @@ class MediaPlayerController( controller?.clearMediaItems() if (controller != null && serialize) { - playbackStateSerializer.serialize( - listOf(), -1, 0 + playbackStateSerializer.serializeAsync( + listOf(), -1, 0, isShufflePlayEnabled, repeatMode ) } @@ -481,10 +482,12 @@ class MediaPlayerController( // Don't serialize invalid sessions if (currentMediaItemIndex == -1) return - playbackStateSerializer.serialize( - legacyPlaylistManager.playlist, - currentMediaItemIndex, - playerPosition + playbackStateSerializer.serializeAsync( + songs = legacyPlaylistManager.playlist, + currentPlayingIndex = currentMediaItemIndex, + currentPlayingPosition = playerPosition, + isShufflePlayEnabled, + repeatMode ) } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt index 2700747b..01bd8af7 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt @@ -60,9 +60,7 @@ class MediaPlayerLifecycleSupport : KoinComponent { Timber.i("Restoring %s songs", it!!.songs.size) mediaPlayerController.restore( - it.songs, - it.currentPlayingIndex, - it.currentPlayingPosition, + it, autoPlay, false ) @@ -78,7 +76,9 @@ class MediaPlayerLifecycleSupport : KoinComponent { playbackStateSerializer.serializeNow( mediaPlayerController.playList, mediaPlayerController.currentMediaItemIndex, - mediaPlayerController.playerPosition + mediaPlayerController.playerPosition, + mediaPlayerController.isShufflePlayEnabled, + mediaPlayerController.repeatMode ) mediaPlayerController.clear(false) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/PlaybackStateSerializer.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/PlaybackStateSerializer.kt index 552c399c..d67f9d8f 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/PlaybackStateSerializer.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/PlaybackStateSerializer.kt @@ -31,17 +31,25 @@ class PlaybackStateSerializer : KoinComponent { private val ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val mainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) - fun serialize( + fun serializeAsync( songs: Iterable, currentPlayingIndex: Int, - currentPlayingPosition: Int + currentPlayingPosition: Int, + shufflePlay: Boolean, + repeatMode: Int ) { if (isSerializing.get() || !isSetup.get()) return isSerializing.set(true) ioScope.launch { - serializeNow(songs, currentPlayingIndex, currentPlayingPosition) + serializeNow( + songs, + currentPlayingIndex, + currentPlayingPosition, + shufflePlay, + repeatMode + ) }.invokeOnCompletion { isSerializing.set(false) } @@ -50,17 +58,22 @@ class PlaybackStateSerializer : KoinComponent { fun serializeNow( referencedList: Iterable, currentPlayingIndex: Int, - currentPlayingPosition: Int + currentPlayingPosition: Int, + shufflePlay: Boolean, + repeatMode: Int ) { - val state = State() - val songs = referencedList.toList() - for (downloadFile in songs) { - state.songs.add(downloadFile.track) + val tracks = referencedList.toList().map { + it.track } - state.currentPlayingIndex = currentPlayingIndex - state.currentPlayingPosition = currentPlayingPosition + val state = PlaybackState( + tracks, + currentPlayingIndex, + currentPlayingPosition, + shufflePlay, + repeatMode + ) Timber.i( "Serialized currentPlayingIndex: %d, currentPlayingPosition: %d", @@ -71,7 +84,7 @@ class PlaybackStateSerializer : KoinComponent { FileUtil.serialize(context, state, Constants.FILENAME_PLAYLIST_SER) } - fun deserialize(afterDeserialized: (State?) -> Unit?) { + fun deserialize(afterDeserialized: (PlaybackState?) -> Unit?) { if (isDeserializing.get()) return ioScope.launch { try { @@ -85,9 +98,9 @@ class PlaybackStateSerializer : KoinComponent { } } - private fun deserializeNow(afterDeserialized: (State?) -> Unit?) { + private fun deserializeNow(afterDeserialized: (PlaybackState?) -> Unit?) { - val state = FileUtil.deserialize( + val state = FileUtil.deserialize( context, Constants.FILENAME_PLAYLIST_SER ) ?: return