fix: respect preference to not start playing automatically everywhere

This commit is contained in:
Bnyro 2023-08-04 18:38:45 +02:00
parent d3d78a93ce
commit 3495649669
6 changed files with 22 additions and 12 deletions

View File

@ -322,6 +322,13 @@ object PlayerHelper {
"all" "all"
) )
val playAutomatically: Boolean
get() = PreferenceHelper
.getBoolean(
PreferenceKeys.PLAY_AUTOMATICALLY,
true
)
fun getDefaultResolution(context: Context): String { fun getDefaultResolution(context: Context): String {
val prefKey = if (NetworkHelper.isNetworkMetered(context)) { val prefKey = if (NetworkHelper.isNetworkMetered(context)) {
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE PreferenceKeys.DEFAULT_RESOLUTION_MOBILE

View File

@ -83,9 +83,7 @@ class OfflinePlayerService : LifecycleService() {
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true) .setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setLoadControl(PlayerHelper.getLoadControl()) .setLoadControl(PlayerHelper.getLoadControl())
.build() .build()
.loadPlaybackParams(isBackgroundMode = true).apply { .loadPlaybackParams(isBackgroundMode = true)
playWhenReady = true
}
val audioItem = downloadWithItem.downloadItems.firstOrNull { it.type == FileType.AUDIO } val audioItem = downloadWithItem.downloadItems.firstOrNull { it.type == FileType.AUDIO }
?: // in some rare cases, video files can contain audio ?: // in some rare cases, video files can contain audio
@ -96,6 +94,7 @@ class OfflinePlayerService : LifecycleService() {
.build() .build()
player?.setMediaItem(mediaItem) player?.setMediaItem(mediaItem)
player?.playWhenReady = PlayerHelper.playAutomatically
player?.prepare() player?.prepare()
return true return true
} }

View File

@ -216,7 +216,7 @@ class OnlinePlayerService : LifecycleService() {
streams?.let { onNewVideo?.invoke(it, videoId) } streams?.let { onNewVideo?.invoke(it, videoId) }
player?.apply { player?.apply {
playWhenReady = true playWhenReady = PlayerHelper.playAutomatically
prepare() prepare()
} }

View File

@ -156,8 +156,8 @@ class OfflinePlayerActivity : BaseActivity() {
OfflineTimeFrameReceiver(this@OfflinePlayerActivity, it) OfflineTimeFrameReceiver(this@OfflinePlayerActivity, it)
} }
player.playWhenReady = PlayerHelper.playAutomatically
player.prepare() player.prepare()
player.play()
} }
} }

View File

@ -59,7 +59,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
private var transitionEndId = 0 private var transitionEndId = 0
private var handler = Handler(Looper.getMainLooper()) private var handler = Handler(Looper.getMainLooper())
private var isPaused = false private var isPaused = !PlayerHelper.playAutomatically
private var playerService: OnlinePlayerService? = null private var playerService: OnlinePlayerService? = null
@ -204,6 +204,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
binding.volumeProgressBar.let { bar -> binding.volumeProgressBar.let { bar ->
bar.progress = audioHelper.getVolumeWithScale(bar.max) bar.progress = audioHelper.getVolumeWithScale(bar.max)
} }
if (!PlayerHelper.playAutomatically) updatePlayPauseButton(false)
} }
private fun killFragment() { private fun killFragment() {
@ -320,11 +322,15 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
handler.postDelayed(this::updateSeekBar, 200) handler.postDelayed(this::updateSeekBar, 200)
} }
private fun handleServiceConnection() { private fun updatePlayPauseButton(isPlaying: Boolean) {
playerService?.onIsPlayingChanged = { isPlaying ->
val iconResource = if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play val iconResource = if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play
binding.playPause.setIconResource(iconResource) binding.playPause.setIconResource(iconResource)
binding.miniPlayerPause.setImageResource(iconResource) binding.miniPlayerPause.setImageResource(iconResource)
}
private fun handleServiceConnection() {
playerService?.onIsPlayingChanged = { isPlaying ->
updatePlayPauseButton(isPlaying)
isPaused = !isPlaying isPaused = !isPlaying
} }
playerService?.onNewVideo = { streams, videoId -> playerService?.onNewVideo = { streams, videoId ->

View File

@ -754,10 +754,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (viewModel.isFullscreen.value == true) updateFullscreenOrientation() if (viewModel.isFullscreen.value == true) updateFullscreenOrientation()
exoPlayer.playWhenReady = PlayerHelper.playAutomatically
exoPlayer.prepare() exoPlayer.prepare()
if (PreferenceHelper.getBoolean(PreferenceKeys.PLAY_AUTOMATICALLY, true)) {
exoPlayer.play()
}
if (binding.playerMotionLayout.progress != 1.0f) { if (binding.playerMotionLayout.progress != 1.0f) {
// show controllers when not in picture in picture mode // show controllers when not in picture in picture mode