Fix that the info in the audio player doesn't update

This commit is contained in:
Bnyro 2023-06-01 18:03:59 +02:00
parent 10383c8ba6
commit 6d90c7d69c
3 changed files with 7 additions and 28 deletions

View File

@ -94,6 +94,7 @@ class OnlinePlayerService : LifecycleService() {
* Listener for passing playback state changes to the AudioPlayerFragment * Listener for passing playback state changes to the AudioPlayerFragment
*/ */
var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null
var onNewVideo: ((streams: Streams, videoId: String) -> Unit)? = null
/** /**
* Setting the required [Notification] for running as a foreground service * Setting the required [Notification] for running as a foreground service
@ -203,6 +204,7 @@ class OnlinePlayerService : LifecycleService() {
streams?.thumbnailUrl, streams?.thumbnailUrl,
) )
nowPlayingNotification.updatePlayerNotification(videoId, playerNotificationData) nowPlayingNotification.updatePlayerNotification(videoId, playerNotificationData)
streams?.let { onNewVideo?.invoke(it, videoId) }
player?.apply { player?.apply {
playWhenReady = playWhenReadyPlayer playWhenReady = playWhenReadyPlayer

View File

@ -53,9 +53,6 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
private var sId: Int = 0 private var sId: Int = 0
private var eId: Int = 0 private var eId: Int = 0
private val onTrackChangeListener: (StreamItem) -> Unit = {
updateStreamInfo()
}
private var handler = Handler(Looper.getMainLooper()) private var handler = Handler(Looper.getMainLooper())
private var isPaused: Boolean = false private var isPaused: Boolean = false
@ -170,9 +167,6 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
val listener = AudioPlayerThumbnailListener(requireContext(), this) val listener = AudioPlayerThumbnailListener(requireContext(), this)
binding.thumbnail.setOnTouchListener(listener) binding.thumbnail.setOnTouchListener(listener)
// Listen for track changes due to autoplay or the notification
PlayingQueue.addOnTrackChangedListener(onTrackChangeListener)
binding.playPause.setOnClickListener { binding.playPause.setOnClickListener {
if (isPaused) playerService?.play() else playerService?.pause() if (isPaused) playerService?.play() else playerService?.pause()
} }
@ -233,9 +227,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
/** /**
* Load the information from a new stream into the UI * Load the information from a new stream into the UI
*/ */
private fun updateStreamInfo() { private fun updateStreamInfo(stream: StreamItem? = null) {
val current = PlayingQueue.getCurrent() val current = stream ?: PlayingQueue.getCurrent() ?: return
current ?: return
binding.title.text = current.title binding.title.text = current.title
binding.miniPlayerTitle.text = current.title binding.miniPlayerTitle.text = current.title
@ -307,6 +300,9 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
binding.miniPlayerPause.setImageResource(iconResource) binding.miniPlayerPause.setImageResource(iconResource)
isPaused = !isPlaying isPaused = !isPlaying
} }
playerService?.onNewVideo = { streams, videoId ->
updateStreamInfo(streams.toStreamItem(videoId))
}
initializeSeekBar() initializeSeekBar()
} }
@ -321,7 +317,6 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
runCatching { runCatching {
activity?.unbindService(connection) activity?.unbindService(connection)
} }
PlayingQueue.removeOnTrackChangedListener(onTrackChangeListener)
super.onDestroy() super.onDestroy()
} }

View File

@ -20,10 +20,6 @@ object PlayingQueue {
*/ */
private var onQueueTapListener: (StreamItem) -> Unit = {} private var onQueueTapListener: (StreamItem) -> Unit = {}
/**
* Listener that gets called when the current playing video changes
*/
private val onTrackChangedListeners: MutableList<(StreamItem) -> Unit> = mutableListOf()
var repeatQueue: Boolean = false var repeatQueue: Boolean = false
fun clear() = queue.clear() fun clear() = queue.clear()
@ -61,11 +57,6 @@ object PlayingQueue {
fun updateCurrent(streamItem: StreamItem) { fun updateCurrent(streamItem: StreamItem) {
currentStream = streamItem currentStream = streamItem
onTrackChangedListeners.forEach {
runCatching {
it.invoke(streamItem)
}
}
if (!contains(streamItem)) queue.add(0, streamItem) if (!contains(streamItem)) queue.add(0, streamItem)
} }
@ -173,17 +164,8 @@ object PlayingQueue {
onQueueTapListener = listener onQueueTapListener = listener
} }
fun addOnTrackChangedListener(listener: (StreamItem) -> Unit) {
onTrackChangedListeners.add(listener)
}
fun removeOnTrackChangedListener(listener: (StreamItem) -> Unit) {
onTrackChangedListeners.remove(listener)
}
fun resetToDefaults() { fun resetToDefaults() {
repeatQueue = false repeatQueue = false
onQueueTapListener = {} onQueueTapListener = {}
onTrackChangedListeners.clear()
} }
} }