mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Merge pull request #5155 from Bnyro/master
refactor: set the wake mode everywhere and simplify player listenern inits
This commit is contained in:
commit
8900aed8da
@ -103,6 +103,48 @@ class OnlinePlayerService : LifecycleService() {
|
|||||||
var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null
|
var onIsPlayingChanged: ((isPlaying: Boolean) -> Unit)? = null
|
||||||
var onNewVideo: ((streams: Streams, videoId: String) -> 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
|
* 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)
|
* Listens for changed playbackStates (e.g. pause, end)
|
||||||
* Plays the next video when the current one ended
|
* Plays the next video when the current one ended
|
||||||
*/
|
*/
|
||||||
player?.addListener(object : Player.Listener {
|
player?.addListener(playerListener)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,6 +54,30 @@ class OfflinePlayerActivity : BaseActivity() {
|
|||||||
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
|
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
|
||||||
private val playerViewModel: PlayerViewModel by viewModels()
|
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
WindowHelper.toggleFullscreen(window, true)
|
WindowHelper.toggleFullscreen(window, true)
|
||||||
|
|
||||||
@ -79,30 +103,8 @@ class OfflinePlayerActivity : BaseActivity() {
|
|||||||
trackSelector = DefaultTrackSelector(this)
|
trackSelector = DefaultTrackSelector(this)
|
||||||
|
|
||||||
player = PlayerHelper.createPlayer(this, trackSelector, false)
|
player = PlayerHelper.createPlayer(this, trackSelector, false)
|
||||||
|
player.setWakeMode(C.WAKE_MODE_LOCAL)
|
||||||
player.addListener(object : Player.Listener {
|
player.addListener(playerListener)
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
playerView = binding.player
|
playerView = binding.player
|
||||||
playerView.setShowSubtitleButton(true)
|
playerView.setShowSubtitleButton(true)
|
||||||
|
@ -37,6 +37,7 @@ import androidx.fragment.app.commit
|
|||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.media3.common.C
|
import androidx.media3.common.C
|
||||||
|
import androidx.media3.common.C.WakeMode
|
||||||
import androidx.media3.common.MediaItem
|
import androidx.media3.common.MediaItem
|
||||||
import androidx.media3.common.MediaItem.SubtitleConfiguration
|
import androidx.media3.common.MediaItem.SubtitleConfiguration
|
||||||
import androidx.media3.common.MimeTypes
|
import androidx.media3.common.MimeTypes
|
||||||
@ -1361,6 +1362,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
PlayerHelper.applyPreferredAudioQuality(requireContext(), trackSelector)
|
PlayerHelper.applyPreferredAudioQuality(requireContext(), trackSelector)
|
||||||
|
|
||||||
exoPlayer = PlayerHelper.createPlayer(requireContext(), trackSelector, false)
|
exoPlayer = PlayerHelper.createPlayer(requireContext(), trackSelector, false)
|
||||||
|
exoPlayer.setWakeMode(C.WAKE_MODE_NETWORK)
|
||||||
exoPlayer.addListener(playerListener)
|
exoPlayer.addListener(playerListener)
|
||||||
viewModel.player = exoPlayer
|
viewModel.player = exoPlayer
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user