Only updates video position when the video is ready to play

This commit is contained in:
Vitor Pamplona
2024-06-07 11:44:50 -04:00
parent 7906a94ca0
commit 0e1de03db8
2 changed files with 12 additions and 6 deletions
@@ -156,7 +156,7 @@ class MultiPlayerPlaybackManager(
newPosition: PositionInfo, newPosition: PositionInfo,
reason: Int, reason: Int,
) { ) {
if (mustCachePositions) { if (mustCachePositions && player.playbackState != STATE_IDLE) {
cachedPositions.add(uri, newPosition.positionMs) cachedPositions.add(uri, newPosition.positionMs)
} }
} }
@@ -508,14 +508,20 @@ fun GetVideoController(
} }
onDispose { onDispose {
GlobalScope.launch(Dispatchers.Main) { if (!keepPlaying.value) {
if (!keepPlaying.value) { // Makes sure the variable is cleared before the task is launched
// Stops and releases the media. // to avoid the ON_RELEASE running before ON_PAUSE's coroutine
controller.value?.let { val toRelease = controller.value
controller.value = null
toRelease?.let {
it.pause()
GlobalScope.launch(Dispatchers.Main) {
// Stops and releases the media.
it.stop() it.stop()
it.release() it.release()
Log.d("PlaybackService", "Releasing Video $videoUri ") Log.d("PlaybackService", "Releasing Video $videoUri ")
controller.value = null
} }
} }
} }