From 34956496690999b0f97cd9ec5e3b9961a9bf283f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 4 Aug 2023 18:38:45 +0200 Subject: [PATCH] fix: respect preference to not start playing automatically everywhere --- .../com/github/libretube/helpers/PlayerHelper.kt | 7 +++++++ .../libretube/services/OfflinePlayerService.kt | 5 ++--- .../libretube/services/OnlinePlayerService.kt | 2 +- .../ui/activities/OfflinePlayerActivity.kt | 2 +- .../libretube/ui/fragments/AudioPlayerFragment.kt | 14 ++++++++++---- .../libretube/ui/fragments/PlayerFragment.kt | 4 +--- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index fc9f63d65..dd80d0365 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -322,6 +322,13 @@ object PlayerHelper { "all" ) + val playAutomatically: Boolean + get() = PreferenceHelper + .getBoolean( + PreferenceKeys.PLAY_AUTOMATICALLY, + true + ) + fun getDefaultResolution(context: Context): String { val prefKey = if (NetworkHelper.isNetworkMetered(context)) { PreferenceKeys.DEFAULT_RESOLUTION_MOBILE diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index 70815d03e..49d82885e 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -83,9 +83,7 @@ class OfflinePlayerService : LifecycleService() { .setAudioAttributes(PlayerHelper.getAudioAttributes(), true) .setLoadControl(PlayerHelper.getLoadControl()) .build() - .loadPlaybackParams(isBackgroundMode = true).apply { - playWhenReady = true - } + .loadPlaybackParams(isBackgroundMode = true) val audioItem = downloadWithItem.downloadItems.firstOrNull { it.type == FileType.AUDIO } ?: // in some rare cases, video files can contain audio @@ -96,6 +94,7 @@ class OfflinePlayerService : LifecycleService() { .build() player?.setMediaItem(mediaItem) + player?.playWhenReady = PlayerHelper.playAutomatically player?.prepare() return true } diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index c9fb025da..507913860 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -216,7 +216,7 @@ class OnlinePlayerService : LifecycleService() { streams?.let { onNewVideo?.invoke(it, videoId) } player?.apply { - playWhenReady = true + playWhenReady = PlayerHelper.playAutomatically prepare() } diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 08c3c0cb7..e402fbed5 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -156,8 +156,8 @@ class OfflinePlayerActivity : BaseActivity() { OfflineTimeFrameReceiver(this@OfflinePlayerActivity, it) } + player.playWhenReady = PlayerHelper.playAutomatically player.prepare() - player.play() } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt index a5a811d5b..c2683637c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt @@ -59,7 +59,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { private var transitionEndId = 0 private var handler = Handler(Looper.getMainLooper()) - private var isPaused = false + private var isPaused = !PlayerHelper.playAutomatically private var playerService: OnlinePlayerService? = null @@ -204,6 +204,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { binding.volumeProgressBar.let { bar -> bar.progress = audioHelper.getVolumeWithScale(bar.max) } + + if (!PlayerHelper.playAutomatically) updatePlayPauseButton(false) } private fun killFragment() { @@ -320,11 +322,15 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { handler.postDelayed(this::updateSeekBar, 200) } + private fun updatePlayPauseButton(isPlaying: Boolean) { + val iconResource = if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play + binding.playPause.setIconResource(iconResource) + binding.miniPlayerPause.setImageResource(iconResource) + } + private fun handleServiceConnection() { playerService?.onIsPlayingChanged = { isPlaying -> - val iconResource = if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play - binding.playPause.setIconResource(iconResource) - binding.miniPlayerPause.setImageResource(iconResource) + updatePlayPauseButton(isPlaying) isPaused = !isPlaying } playerService?.onNewVideo = { streams, videoId -> diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index b6887e598..2130613a8 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -754,10 +754,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { if (viewModel.isFullscreen.value == true) updateFullscreenOrientation() + exoPlayer.playWhenReady = PlayerHelper.playAutomatically exoPlayer.prepare() - if (PreferenceHelper.getBoolean(PreferenceKeys.PLAY_AUTOMATICALLY, true)) { - exoPlayer.play() - } if (binding.playerMotionLayout.progress != 1.0f) { // show controllers when not in picture in picture mode