feat: allow resuming playback after disconnect

This commit is contained in:
Bnyro 2023-09-10 14:33:28 +02:00
parent 087a39757f
commit 1d236ded79
4 changed files with 24 additions and 16 deletions

View File

@ -0,0 +1,19 @@
package com.github.libretube.extensions
import androidx.media3.common.Player
fun Player.togglePlayPauseState() {
when {
playerError != null -> {
prepare()
play()
}
!isPlaying && playbackState == Player.STATE_ENDED -> {
seekTo(0)
}
!isPlaying && !isLoading -> play()
else -> pause()
}
}

View File

@ -79,6 +79,7 @@ import com.github.libretube.extensions.serializableExtra
import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.extensions.togglePlayPauseState
import com.github.libretube.extensions.updateParameters import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
@ -391,14 +392,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
playerBinding.autoPlay.isVisible = true playerBinding.autoPlay.isVisible = true
binding.playImageView.setOnClickListener { binding.playImageView.setOnClickListener {
when { exoPlayer.togglePlayPauseState()
!exoPlayer.isPlaying && exoPlayer.playbackState == Player.STATE_ENDED -> {
exoPlayer.seekTo(0)
}
!exoPlayer.isPlaying -> exoPlayer.play()
else -> exoPlayer.pause()
}
} }
// video description and chapters toggle // video description and chapters toggle

View File

@ -41,6 +41,7 @@ import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.normalize import com.github.libretube.extensions.normalize
import com.github.libretube.extensions.round import com.github.libretube.extensions.round
import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.togglePlayPauseState
import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.AudioHelper
import com.github.libretube.helpers.BrightnessHelper import com.github.libretube.helpers.BrightnessHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
@ -156,14 +157,7 @@ open class CustomExoPlayerView(
} }
binding.playPauseBTN.setOnClickListener { binding.playPauseBTN.setOnClickListener {
when { player?.togglePlayPauseState()
player?.isPlaying == false && player?.playbackState == Player.STATE_ENDED -> {
player?.seekTo(0)
}
player?.isPlaying == false && player?.isLoading == false -> player?.play()
else -> player?.pause()
}
} }
player?.addListener(object : Player.Listener { player?.addListener(object : Player.Listener {

View File

@ -299,6 +299,7 @@ class NowPlayingNotification(
} }
PLAY_PAUSE -> { PLAY_PAUSE -> {
if (player.playerError != null) player.prepare()
if (player.isPlaying) player.pause() else player.play() if (player.isPlaying) player.pause() else player.play()
} }