Save shuffel/repeat mode in PlaybackState

This commit is contained in:
tzugen 2022-07-05 22:51:37 +02:00
parent bf55e9ebc1
commit 8c160a31eb
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
5 changed files with 67 additions and 48 deletions

View File

@ -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<Track> songs = new ArrayList<>();
public int currentPlayingIndex;
public int currentPlayingPosition;
data class PlaybackState(
val songs: List<Track> = listOf(),
val currentPlayingIndex: Int = 0,
val currentPlayingPosition: Int = 0,
var shufflePlay: Boolean = false,
var repeatMode: Int = 0
) : Serializable {
companion object {
const val serialVersionUID = -293487987L
}
}

View File

@ -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<Track>) {
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
)

View File

@ -256,9 +256,7 @@ class MediaPlayerController(
@Synchronized
fun restore(
songs: List<Track>,
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
)
}

View File

@ -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)

View File

@ -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<DownloadFile>,
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<DownloadFile>,
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<State>(
val state = FileUtil.deserialize<PlaybackState>(
context, Constants.FILENAME_PLAYLIST_SER
) ?: return