[Android] Refactor the repeat mode into a flow and update the AudioPlayer
Nicolas Pomepuy
git at videolan.org
Wed Jul 19 07:48:17 UTC 2023
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jul 19 08:45:00 2023 +0200| [6ec49778dac22ff03cd53782082b8501d69e891e] | committer: Nicolas Pomepuy
Refactor the repeat mode into a flow and update the AudioPlayer
> https://code.videolan.org/videolan/vlc-android/commit/6ec49778dac22ff03cd53782082b8501d69e891e
---
.../src/org/videolan/vlc/PlaybackService.kt | 4 +--
.../src/org/videolan/vlc/gui/audio/AudioPlayer.kt | 6 +++++
.../vlc/gui/video/benchmark/BenchActivity.kt | 7 +++---
.../src/org/videolan/vlc/media/PlaylistManager.kt | 29 +++++++++++++---------
4 files changed, 29 insertions(+), 17 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 7715a1672b..af4c1c4447 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -318,7 +318,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
var repeatType: Int
@MainThread
- get() = playlistManager.repeating
+ get() = PlaylistManager.repeating.value
@MainThread
set(repeatType) {
playlistManager.setRepeatType(if (repeatType == PlaybackStateCompat.REPEAT_MODE_GROUP) PlaybackStateCompat.REPEAT_MODE_ALL else repeatType)
@@ -1099,7 +1099,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
}
pscb.setState(state, time, if (isPaused) 0f else playlistManager.player.getRate())
pscb.setActiveQueueItemId(playlistManager.currentIndex.toLong())
- val repeatType = playlistManager.repeating
+ val repeatType = PlaylistManager.repeating.value
val podcastMode = isPodcastMode
if (repeatType != PlaybackStateCompat.REPEAT_MODE_NONE || hasNext())
actions = actions or PlaybackStateCompat.ACTION_SKIP_TO_NEXT
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 427b2d392f..5b2bb24c82 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -80,6 +80,7 @@ import org.videolan.vlc.gui.video.VideoPlayerActivity
import org.videolan.vlc.gui.view.AudioMediaSwitcher
import org.videolan.vlc.gui.view.AudioMediaSwitcher.AudioMediaSwitcherListener
import org.videolan.vlc.manageAbRepeatStep
+import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.media.PlaylistManager.Companion.hasMedia
import org.videolan.vlc.util.TextUtils
import org.videolan.vlc.util.launchWhenStarted
@@ -145,6 +146,11 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
doUpdate()
}
}
+ lifecycleScope.launchWhenStarted {
+ PlaylistManager.repeating.collect {
+ updateRepeatMode()
+ }
+ }
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/benchmark/BenchActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/benchmark/BenchActivity.kt
index 14fb583eb5..6b29037c86 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/benchmark/BenchActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/benchmark/BenchActivity.kt
@@ -44,6 +44,7 @@ import org.videolan.tools.Settings
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.gui.helpers.restartMediaPlayer
+import org.videolan.vlc.media.PlaylistManager
import java.io.*
/**
@@ -125,11 +126,11 @@ class BenchActivity : ShallowVideoPlayer() {
super.onServiceChanged(service)
if (isSpeed && this.service != null) {
oldRate = service!!.rate
- oldRepeating = service.playlistManager.repeating
+ oldRepeating = PlaylistManager.repeating.value
service.playlistManager.setRepeatType(PlaybackStateCompat.REPEAT_MODE_ONE)
} else if (!isSpeed && this.service != null) {
- oldRepeating = service!!.playlistManager.repeating
- service.playlistManager.setRepeatType(PlaybackStateCompat.REPEAT_MODE_NONE)
+ oldRepeating = PlaylistManager.repeating.value
+ service!!.playlistManager.setRepeatType(PlaybackStateCompat.REPEAT_MODE_NONE)
}
if (isHardware && this.service != null) {
val sharedPref = Settings.getInstance(this)
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 a2928b0a8e..1af756247f 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -13,6 +13,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
+import kotlinx.coroutines.flow.MutableStateFlow
import org.videolan.libvlc.FactoryManager
import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.RendererItem
@@ -52,6 +53,8 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var skipMediaUpdateRefresh = false
private val mediaList = MediaWrapperList()
fun hasMedia() = mediaList.size() != 0
+ val repeating = MutableStateFlow(PlaybackStateCompat.REPEAT_MODE_NONE)
+
}
private val medialibrary by lazy(LazyThreadSafetyMode.NONE) { Medialibrary.getInstance() }
@@ -64,7 +67,6 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var startupIndex = -1
private var previous = Stack<Int>()
var stopAfter = -1
- var repeating = PlaybackStateCompat.REPEAT_MODE_NONE
var shuffling = false
var videoBackground = false
private set
@@ -99,7 +101,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
private var shouldDisableCookieForwarding: Boolean = false
init {
- repeating = settings.getInt(PLAYLIST_AUDIO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)
+ AppScope.launch { repeating.emit(settings.getInt(PLAYLIST_AUDIO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)) }
resetResumeStatus()
}
@@ -292,7 +294,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
return
}
videoBackground = videoBackground || (!player.isVideoPlaying() && player.canSwitchToVideo())
- if (repeating == PlaybackStateCompat.REPEAT_MODE_ONE) {
+ if (repeating.value == PlaybackStateCompat.REPEAT_MODE_ONE) {
setRepeatType(PlaybackStateCompat.REPEAT_MODE_NONE)
}
}
@@ -371,19 +373,22 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
* Will set the repeating variable from the value that has been saved in settings
*/
private fun setRepeatTypeFromSettings() {
- repeating = if (getCurrentMedia()?.type == MediaWrapper.TYPE_VIDEO) {
- settings.getInt(PLAYLIST_VIDEO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)
- } else
- settings.getInt(PLAYLIST_AUDIO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)
+ AppScope.launch {
+ repeating.emit(if (getCurrentMedia()?.type == MediaWrapper.TYPE_VIDEO) {
+ settings.getInt(PLAYLIST_VIDEO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)
+ } else
+ settings.getInt(PLAYLIST_AUDIO_REPEAT_MODE_KEY, PlaybackStateCompat.REPEAT_MODE_NONE)
+ )
+ }
}
@MainThread
fun setRepeatType(repeatType: Int) {
- repeating = repeatType
+ AppScope.launch { repeating.emit(repeatType) }
if (getCurrentMedia()?.type == MediaWrapper.TYPE_VIDEO)
- settings.putSingle(PLAYLIST_VIDEO_REPEAT_MODE_KEY, repeating)
+ settings.putSingle(PLAYLIST_VIDEO_REPEAT_MODE_KEY, repeating.value)
else
- settings.putSingle(PLAYLIST_AUDIO_REPEAT_MODE_KEY, repeating)
+ settings.putSingle(PLAYLIST_AUDIO_REPEAT_MODE_KEY, repeating.value)
savePosition()
launch { determinePrevAndNextIndices() }
}
@@ -732,7 +737,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
// If we've played all songs already in shuffle, then either
// reshuffle or stop (depending on RepeatType).
if (previous.size + 1 == size) {
- if (repeating == PlaybackStateCompat.REPEAT_MODE_NONE) {
+ if (repeating.value == PlaybackStateCompat.REPEAT_MODE_NONE) {
nextIndex = -1
return
} else {
@@ -748,7 +753,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
if (currentIndex > 0) prevIndex = currentIndex - 1
nextIndex = when {
currentIndex + 1 < size -> currentIndex + 1
- repeating == PlaybackStateCompat.REPEAT_MODE_NONE -> -1
+ repeating.value == PlaybackStateCompat.REPEAT_MODE_NONE -> -1
else -> 0
}
}
More information about the Android
mailing list