refactor: set the wake mode everywhere and simplify player listener initializations

This commit is contained in:
Bnyro 2023-11-15 16:59:54 +01:00
parent 981890acb9
commit 4f6531b619
3 changed files with 71 additions and 65 deletions

View File

@ -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)
}
/**

View File

@ -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)

View File

@ -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
}