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 f2b755588..8dd9540b2 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -103,6 +103,48 @@ class OnlinePlayerService : LifecycleService() { var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null var onNewVideo: ((streams: Streams, videoId: String) -> Unit)? = null + private val playerListener = object : Player.Listener { + override fun onIsPlayingChanged(isPlaying: Boolean) { + super.onIsPlayingChanged(isPlaying) + onIsPlayingChanged?.invoke(isPlaying) + } + + override fun onPlaybackStateChanged(state: Int) { + when (state) { + Player.STATE_ENDED -> { + if (PlayerHelper.shouldPlayNextVideo(playlistId != null) && !isTransitioning) playNextVideo() + } + + Player.STATE_IDLE -> { + onDestroy() + } + + Player.STATE_BUFFERING -> {} + Player.STATE_READY -> { + isTransitioning = false + + // save video to watch history when the video starts playing or is being resumed + // waiting for the player to be ready since the video can't be claimed to be watched + // while it did not yet start actually, but did buffer only so far + lifecycleScope.launch(Dispatchers.IO) { + streams?.let { DatabaseHelper.addToWatchHistory(videoId, it) } + } + } + } + } + + override fun onPlayerError(error: PlaybackException) { + // show a toast on errors + Handler(Looper.getMainLooper()).post { + Toast.makeText( + this@OnlinePlayerService.applicationContext, + error.localizedMessage, + Toast.LENGTH_SHORT + ).show() + } + } + } + /** * Setting the required [Notification] for running as a foreground service */ @@ -255,47 +297,7 @@ class OnlinePlayerService : LifecycleService() { * Listens for changed playbackStates (e.g. pause, end) * Plays the next video when the current one ended */ - player?.addListener(object : Player.Listener { - override fun onIsPlayingChanged(isPlaying: Boolean) { - super.onIsPlayingChanged(isPlaying) - onIsPlayingChanged?.invoke(isPlaying) - } - - override fun onPlaybackStateChanged(state: Int) { - when (state) { - Player.STATE_ENDED -> { - if (PlayerHelper.shouldPlayNextVideo(playlistId != null) && !isTransitioning) playNextVideo() - } - - Player.STATE_IDLE -> { - onDestroy() - } - - Player.STATE_BUFFERING -> {} - Player.STATE_READY -> { - isTransitioning = false - - // save video to watch history when the video starts playing or is being resumed - // waiting for the player to be ready since the video can't be claimed to be watched - // while it did not yet start actually, but did buffer only so far - lifecycleScope.launch(Dispatchers.IO) { - streams?.let { DatabaseHelper.addToWatchHistory(videoId, it) } - } - } - } - } - - override fun onPlayerError(error: PlaybackException) { - // show a toast on errors - Handler(Looper.getMainLooper()).post { - Toast.makeText( - this@OnlinePlayerService.applicationContext, - error.localizedMessage, - Toast.LENGTH_SHORT - ).show() - } - } - }) + player?.addListener(playerListener) } /** 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 f16806153..89cec426b 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 @@ -54,6 +54,30 @@ class OfflinePlayerActivity : BaseActivity() { private lateinit var playerBinding: ExoStyledPlayerControlViewBinding private val playerViewModel: PlayerViewModel by viewModels() + private val playerListener = object : Player.Listener { + override fun onEvents(player: Player, events: Player.Events) { + super.onEvents(player, events) + // update the displayed duration on changes + playerBinding.duration.text = DateUtils.formatElapsedTime( + player.duration / 1000 + ) + } + + override fun onPlaybackStateChanged(playbackState: Int) { + super.onPlaybackStateChanged(playbackState) + // setup seekbar preview + if (playbackState == Player.STATE_READY) { + binding.player.binding.exoProgress.addListener( + SeekbarPreviewListener( + timeFrameReceiver ?: return, + binding.player.binding, + player.duration + ) + ) + } + } + } + override fun onCreate(savedInstanceState: Bundle?) { WindowHelper.toggleFullscreen(window, true) @@ -79,30 +103,8 @@ class OfflinePlayerActivity : BaseActivity() { trackSelector = DefaultTrackSelector(this) player = PlayerHelper.createPlayer(this, trackSelector, false) - - player.addListener(object : Player.Listener { - override fun onEvents(player: Player, events: Player.Events) { - super.onEvents(player, events) - // update the displayed duration on changes - playerBinding.duration.text = DateUtils.formatElapsedTime( - player.duration / 1000 - ) - } - - override fun onPlaybackStateChanged(playbackState: Int) { - super.onPlaybackStateChanged(playbackState) - // setup seekbar preview - if (playbackState == Player.STATE_READY) { - binding.player.binding.exoProgress.addListener( - SeekbarPreviewListener( - timeFrameReceiver ?: return, - binding.player.binding, - player.duration - ) - ) - } - } - }) + player.setWakeMode(C.WAKE_MODE_LOCAL) + player.addListener(playerListener) playerView = binding.player playerView.setShowSubtitleButton(true) 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 5f6176c94..05ec6377a 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 @@ -37,6 +37,7 @@ import androidx.fragment.app.commit import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.media3.common.C +import androidx.media3.common.C.WakeMode import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem.SubtitleConfiguration import androidx.media3.common.MimeTypes @@ -1361,6 +1362,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { PlayerHelper.applyPreferredAudioQuality(requireContext(), trackSelector) exoPlayer = PlayerHelper.createPlayer(requireContext(), trackSelector, false) + exoPlayer.setWakeMode(C.WAKE_MODE_NETWORK) exoPlayer.addListener(playerListener) viewModel.player = exoPlayer }