Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fix player state bug ( closes #29 )
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Aug 24, 2023
1 parent 1d79bdf commit da74f18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 1 addition & 45 deletions app/src/main/java/app/suhasdissa/memerize/utils/PlayerState.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,43 +9,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

@Composable
fun Player.isPlayingState(): State<PlayerState> {
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<Pair<Long, Long?>> {
return produceState(
Expand Down Expand Up @@ -86,10 +42,10 @@ fun Player.positionAndDurationState(): State<Pair<Long, Long?>> {

val pollJob = launch {
while (isActive) {
delay(1000)
if (!isSeeking) {
value = currentPosition to duration.let { if (it < 0) null else it }
}
delay(1000)
}
}
if (!isActive) {
Expand Down

0 comments on commit da74f18

Please sign in to comment.