From da74f18c722434016abd3424a067e676cc235534 Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Thu, 24 Aug 2023 11:28:56 +0530 Subject: [PATCH] Fix player state bug ( closes #29 ) --- .../ui/screens/secondary/VideoView.kt | 38 ++++++++++++++- .../suhasdissa/memerize/utils/PlayerState.kt | 46 +------------------ 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/screens/secondary/VideoView.kt b/app/src/main/java/app/suhasdissa/memerize/ui/screens/secondary/VideoView.kt index 64d488c..ad070d5 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/screens/secondary/VideoView.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/screens/secondary/VideoView.kt @@ -66,7 +66,6 @@ import app.suhasdissa.memerize.backend.viewmodels.DownloadState import app.suhasdissa.memerize.backend.viewmodels.PlayerViewModel import app.suhasdissa.memerize.backend.viewmodels.playPause import app.suhasdissa.memerize.utils.PlayerState -import app.suhasdissa.memerize.utils.isPlayingState import app.suhasdissa.memerize.utils.openBrowser import app.suhasdissa.memerize.utils.positionAndDurationState import app.suhasdissa.memerize.utils.shareUrl @@ -261,7 +260,42 @@ fun PlayerController( ), shape = CircleShape ) { - val playState by isPlayingState() + var playState by remember { + mutableStateOf( + if (isPlaying) { + PlayerState.Play + } else if (playbackState == 2 || playbackState == 1) { + PlayerState.Buffer + } else { + PlayerState.Pause + } + ) + } + + DisposableEffect(this) { + val listener = object : Player.Listener { + override fun onPlaybackStateChanged(playbackState: Int) { + if (playbackState == 2) { + playState = PlayerState.Buffer + } + } + + override fun onIsPlayingChanged(isPlaying: Boolean) { + playbackState + playState = if (isPlaying) { + PlayerState.Play + } else if (playbackState == 2) { + PlayerState.Buffer + } else { + PlayerState.Pause + } + } + } + addListener(listener) + onDispose { + removeListener(listener) + } + } IconButton( onClick = { view.playSoundEffect(SoundEffectConstants.CLICK) diff --git a/app/src/main/java/app/suhasdissa/memerize/utils/PlayerState.kt b/app/src/main/java/app/suhasdissa/memerize/utils/PlayerState.kt index 206bc7a..ca8a729 100644 --- a/app/src/main/java/app/suhasdissa/memerize/utils/PlayerState.kt +++ b/app/src/main/java/app/suhasdissa/memerize/utils/PlayerState.kt @@ -1,10 +1,3 @@ -/******************************************************************************* -Created By Suhas Dissanayake on 8/3/23, 11:40 AM -Copyright (c) 2023 -https://github.com/SuhasDissa/ -All Rights Reserved - ******************************************************************************/ - package app.suhasdissa.memerize.utils import androidx.compose.runtime.Composable @@ -16,43 +9,6 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -@Composable -fun Player.isPlayingState(): State { - return produceState( - initialValue = if (isPlaying) { - PlayerState.Play - } else if (playbackState == 2 || playbackState == 1) { - PlayerState.Buffer - } else { - PlayerState.Pause - }, - this - ) { - val listener = object : Player.Listener { - override fun onPlaybackStateChanged(playbackState: Int) { - if (playbackState == 2) { - value = PlayerState.Buffer - } - } - - override fun onIsPlayingChanged(isPlaying: Boolean) { - playbackState - value = if (isPlaying) { - PlayerState.Play - } else if (playbackState == 2) { - PlayerState.Buffer - } else { - PlayerState.Pause - } - } - } - addListener(listener) - if (!isActive) { - removeListener(listener) - } - } -} - @Composable fun Player.positionAndDurationState(): State> { return produceState( @@ -86,10 +42,10 @@ fun Player.positionAndDurationState(): State> { val pollJob = launch { while (isActive) { - delay(1000) if (!isSeeking) { value = currentPosition to duration.let { if (it < 0) null else it } } + delay(1000) } } if (!isActive) {