Fix seek position when listening another voice broadcast

This commit is contained in:
Florian Renaud 2022-11-07 14:52:53 +01:00
parent be18f4ec78
commit 9e83d88f08
1 changed files with 34 additions and 16 deletions

View File

@ -216,8 +216,10 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
currentMediaPlayer?.pause() currentMediaPlayer?.pause()
} else { } else {
stopPlayer() stopPlayer()
currentVoiceBroadcast?.voiceBroadcastId?.let { id -> val voiceBroadcastId = currentVoiceBroadcast?.voiceBroadcastId
playbackTracker.updatePausedAtPlaybackTime(id, positionMillis, positionMillis.toFloat() / playlist.duration) val duration = playlist.duration.takeIf { it > 0 }
if (voiceBroadcastId != null && duration != null) {
playbackTracker.updatePausedAtPlaybackTime(voiceBroadcastId, positionMillis, positionMillis.toFloat() / duration)
} }
} }
playingState = State.PAUSED playingState = State.PAUSED
@ -312,6 +314,22 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
} }
} }
private fun getCurrentPlaybackPosition(): Int? {
val playlistPosition = playlist.currentItem?.startTime
val computedPosition = currentMediaPlayer?.currentPosition?.let { playlistPosition?.plus(it) } ?: playlistPosition
val savedPosition = currentVoiceBroadcast?.voiceBroadcastId?.let { playbackTracker.getPlaybackTime(it) }
return computedPosition ?: savedPosition
}
private fun getCurrentPlaybackPercentage(): Float? {
val playlistPosition = playlist.currentItem?.startTime
val computedPosition = currentMediaPlayer?.currentPosition?.let { playlistPosition?.plus(it) } ?: playlistPosition
val duration = playlist.duration.takeIf { it > 0 }
val computedPercentage = if (computedPosition != null && duration != null) computedPosition.toFloat() / duration else null
val savedPercentage = currentVoiceBroadcast?.voiceBroadcastId?.let { playbackTracker.getPercentage(it) }
return computedPercentage ?: savedPercentage
}
private inner class MediaPlayerListener : private inner class MediaPlayerListener :
MediaPlayer.OnInfoListener, MediaPlayer.OnInfoListener,
MediaPlayer.OnCompletionListener, MediaPlayer.OnCompletionListener,
@ -369,26 +387,26 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
} }
private fun onPlaybackTick(id: String) { private fun onPlaybackTick(id: String) {
val currentItem = playlist.currentItem ?: return val playbackTime = getCurrentPlaybackPosition()
val itemStartTime = currentItem.startTime val percentage = getCurrentPlaybackPercentage()
when (playingState) { when (playingState) {
State.PLAYING, State.PLAYING -> {
State.PAUSED -> { if (playbackTime != null && percentage != null) {
val position = itemStartTime + (currentMediaPlayer?.currentPosition ?: 0) playbackTracker.updatePlayingAtPlaybackTime(id, playbackTime, percentage)
val percentage = position.toFloat() / playlist.duration
if (playingState == State.PLAYING) {
playbackTracker.updatePlayingAtPlaybackTime(id, position, percentage)
} else {
playbackTracker.updatePausedAtPlaybackTime(id, position, percentage)
} }
} }
State.PAUSED,
State.BUFFERING -> { State.BUFFERING -> {
val playbackTime = playbackTracker.getPlaybackTime(id) if (playbackTime != null && percentage != null) {
val percentage = playbackTracker.getPercentage(id) playbackTracker.updatePausedAtPlaybackTime(id, playbackTime, percentage)
playbackTracker.updatePausedAtPlaybackTime(id, playbackTime, percentage) }
} }
State.IDLE -> { State.IDLE -> {
playbackTracker.stopPlayback(id) if (playbackTime == null || percentage == null || playbackTime == playlist.duration) {
playbackTracker.stopPlayback(id)
} else {
playbackTracker.updatePausedAtPlaybackTime(id, playbackTime, percentage)
}
} }
} }
} }