Skip to content

Commit

Permalink
Fix player playback state
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Sep 9, 2023
1 parent b8bf433 commit 17aa50e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ class Stream @Inject constructor(

private val eventListener = object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
logcat { "stream onPlaybackStateChanged: $playbackState" }
if (playbackState == Player.STATE_BUFFERING) {
_flow.value = State.BUFFERING
} else if (isPlaying && playbackState == Player.STATE_READY) {
_flow.value = State.PLAYING
} else if (playbackState == Player.STATE_READY) {
if (isPlaying) {
_flow.value = State.PLAYING
} else {
play()
}
} else if (player?.playWhenReady == true) {
_flow.value = State.PAUSED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class RadioService @Inject constructor(
scope.launchIO {
stream.flow
.collectLatest {
logcat { "stream service flow: $it" }
_state.value = _state.value.copy(
streamState = it,
)
Expand Down Expand Up @@ -127,10 +128,9 @@ class RadioService @Inject constructor(
}

fun togglePlayState() {
if (_state.value.streamState != Stream.State.PAUSED) {
stream.pause()
} else {
stream.play()
when (_state.value.streamState) {
Stream.State.PLAYING, Stream.State.BUFFERING -> stream.pause()
Stream.State.PAUSED, Stream.State.STOPPED -> stream.play()
}
}

Expand Down

0 comments on commit 17aa50e

Please sign in to comment.