player cleanup

This commit is contained in:
Bnyro 2022-10-07 19:30:59 +02:00
parent 5df5a2a9c5
commit ef4e48e347
2 changed files with 146 additions and 143 deletions

View File

@ -67,7 +67,6 @@ import com.github.libretube.ui.views.BottomSheet
import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.AutoPlayHelper
import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NetworkHelper
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
@ -150,24 +149,8 @@ class PlayerFragment : BaseFragment() {
/** /**
* user preferences * user preferences
*/ */
private var token = "" private var token = PreferenceHelper.getToken()
private var relatedStreamsEnabled = true
private var autoRotationEnabled = true
private var pausePlayerOnScreenOffEnabled = false
private var watchHistoryEnabled = true
private var watchPositionsEnabled = true
private var useSystemCaptionStyle = true
private var videoFormatPreference = "webm"
private var defRes = ""
private var bufferingGoal = 50000
private var defaultSubtitleCode = ""
private var sponsorBlockEnabled = true
private var sponsorBlockNotifications = true
private var skipButtonsEnabled = false
private var pipEnabled = true
private var videoShownInExternalPlayer = false private var videoShownInExternalPlayer = false
private var skipSegmentsManually = false
private var progressiveLoadingIntervalSize = "64"
/** /**
* for autoplay * for autoplay
@ -213,7 +196,7 @@ class PlayerFragment : BaseFragment() {
setUserPrefs() setUserPrefs()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
if (autoRotationEnabled) { if (PlayerHelper.autoRotationEnabled) {
// enable auto rotation // enable auto rotation
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
onConfigurationChanged(resources.configuration) onConfigurationChanged(resources.configuration)
@ -244,97 +227,8 @@ class PlayerFragment : BaseFragment() {
token = PreferenceHelper.getToken() token = PreferenceHelper.getToken()
// save whether auto rotation is enabled // save whether auto rotation is enabled
autoRotationEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.AUTO_FULLSCREEN,
false
)
// save whether related streams are enabled // save whether related streams are enabled
relatedStreamsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.RELATED_STREAMS,
true
)
pausePlayerOnScreenOffEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.PAUSE_ON_SCREEN_OFF,
false
)
watchPositionsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_POSITION_TOGGLE,
true
)
watchHistoryEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_HISTORY_TOGGLE,
true
)
useSystemCaptionStyle = PreferenceHelper.getBoolean(
PreferenceKeys.SYSTEM_CAPTION_STYLE,
true
)
videoFormatPreference = PreferenceHelper.getString(
PreferenceKeys.PLAYER_VIDEO_FORMAT,
"webm"
)
defRes = if (NetworkHelper.isNetworkMobile(requireContext())) {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
""
)
} else {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
}
bufferingGoal = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL,
"50"
).toInt() * 1000
sponsorBlockEnabled = PreferenceHelper.getBoolean(
"sb_enabled_key",
true
)
sponsorBlockNotifications = PreferenceHelper.getBoolean(
"sb_notifications_key",
true
)
defaultSubtitleCode = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE,
""
)
if (defaultSubtitleCode.contains("-")) {
defaultSubtitleCode = defaultSubtitleCode.split("-")[0]
}
skipButtonsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.SKIP_BUTTONS,
false
)
pipEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.PICTURE_IN_PICTURE,
true
)
skipSegmentsManually = PreferenceHelper.getBoolean(
PreferenceKeys.SB_SKIP_MANUALLY,
false
)
progressiveLoadingIntervalSize = PreferenceHelper.getString(
PreferenceKeys.PROGRESSIVE_LOADING_INTERVAL_SIZE,
"64"
)
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@ -516,7 +410,7 @@ class PlayerFragment : BaseFragment() {
// FullScreen button trigger // FullScreen button trigger
// hide fullscreen button if auto rotation enabled // hide fullscreen button if auto rotation enabled
playerBinding.fullscreen.visibility = if (autoRotationEnabled) View.GONE else View.VISIBLE playerBinding.fullscreen.visibility = if (PlayerHelper.autoRotationEnabled) View.GONE else View.VISIBLE
playerBinding.fullscreen.setOnClickListener { playerBinding.fullscreen.setOnClickListener {
// hide player controller // hide player controller
exoPlayerView.hideController() exoPlayerView.hideController()
@ -576,7 +470,7 @@ class PlayerFragment : BaseFragment() {
playerBinding.exoTitle.visibility = View.VISIBLE playerBinding.exoTitle.visibility = View.VISIBLE
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
if (!autoRotationEnabled) { if (!PlayerHelper.autoRotationEnabled) {
// different orientations of the video are only available when auto rotation is disabled // different orientations of the video are only available when auto rotation is disabled
val orientation = PlayerHelper.getOrientation(exoPlayer.videoSize) val orientation = PlayerHelper.getOrientation(exoPlayer.videoSize)
mainActivity.requestedOrientation = orientation mainActivity.requestedOrientation = orientation
@ -598,7 +492,7 @@ class PlayerFragment : BaseFragment() {
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen)
playerBinding.exoTitle.visibility = View.INVISIBLE playerBinding.exoTitle.visibility = View.INVISIBLE
if (!autoRotationEnabled) { if (!PlayerHelper.autoRotationEnabled) {
// switch back to portrait mode if auto rotation disabled // switch back to portrait mode if auto rotation disabled
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
@ -652,7 +546,7 @@ class PlayerFragment : BaseFragment() {
// pause player if screen off and setting enabled // pause player if screen off and setting enabled
if ( if (
this::exoPlayer.isInitialized && !isScreenOn && pausePlayerOnScreenOffEnabled this::exoPlayer.isInitialized && !isScreenOn && PlayerHelper.pausePlayerOnScreenOffEnabled
) { ) {
exoPlayer.pause() exoPlayer.pause()
} }
@ -680,19 +574,19 @@ class PlayerFragment : BaseFragment() {
// save the watch position if video isn't finished and option enabled // save the watch position if video isn't finished and option enabled
private fun saveWatchPosition() { private fun saveWatchPosition() {
if (watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) { if (PlayerHelper.watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) {
DatabaseHelper.saveWatchPosition( DatabaseHelper.saveWatchPosition(
videoId!!, videoId!!,
exoPlayer.currentPosition exoPlayer.currentPosition
) )
} else if (watchPositionsEnabled) { } else if (PlayerHelper.watchPositionsEnabled) {
// delete watch position if video has ended // delete watch position if video has ended
DatabaseHelper.removeWatchPosition(videoId!!) DatabaseHelper.removeWatchPosition(videoId!!)
} }
} }
private fun checkForSegments() { private fun checkForSegments() {
if (!exoPlayer.isPlaying || !sponsorBlockEnabled) return if (!exoPlayer.isPlaying || !PlayerHelper.sponsorBlockEnabled) return
Handler(Looper.getMainLooper()).postDelayed(this::checkForSegments, 100) Handler(Looper.getMainLooper()).postDelayed(this::checkForSegments, 100)
@ -705,7 +599,7 @@ class PlayerFragment : BaseFragment() {
// show the button to manually skip the segment // show the button to manually skip the segment
if (currentPosition in segmentStart until segmentEnd) { if (currentPosition in segmentStart until segmentEnd) {
if (skipSegmentsManually) { if (PlayerHelper.skipSegmentsManually) {
binding.sbSkipBtn.visibility = View.VISIBLE binding.sbSkipBtn.visibility = View.VISIBLE
binding.sbSkipBtn.setOnClickListener { binding.sbSkipBtn.setOnClickListener {
exoPlayer.seekTo(segmentEnd) exoPlayer.seekTo(segmentEnd)
@ -713,7 +607,7 @@ class PlayerFragment : BaseFragment() {
return return
} }
if (sponsorBlockNotifications) { if (PlayerHelper.sponsorBlockNotifications) {
Toast Toast
.makeText( .makeText(
context, context,
@ -728,7 +622,7 @@ class PlayerFragment : BaseFragment() {
} }
} }
if (skipSegmentsManually) binding.sbSkipBtn.visibility = View.GONE if (PlayerHelper.skipSegmentsManually) binding.sbSkipBtn.visibility = View.GONE
} }
private fun playVideo() { private fun playVideo() {
@ -768,14 +662,14 @@ class PlayerFragment : BaseFragment() {
} }
// show the player notification // show the player notification
initializePlayerNotification() initializePlayerNotification()
if (sponsorBlockEnabled) fetchSponsorBlockSegments() if (PlayerHelper.sponsorBlockEnabled) fetchSponsorBlockSegments()
// show comments if related streams disabled // show comments if related streams disabled
if (!relatedStreamsEnabled) toggleComments() if (!PlayerHelper.relatedStreamsEnabled) toggleComments()
// prepare for autoplay // prepare for autoplay
if (binding.player.autoplayEnabled) setNextStream() if (binding.player.autoplayEnabled) setNextStream()
// add the video to the watch history // add the video to the watch history
if (watchHistoryEnabled) DatabaseHelper.addToWatchHistory(videoId!!, streams) if (PlayerHelper.watchHistoryEnabled) DatabaseHelper.addToWatchHistory(videoId!!, streams)
} }
} }
} }
@ -881,7 +775,7 @@ class PlayerFragment : BaseFragment() {
player = exoPlayer player = exoPlayer
} }
if (useSystemCaptionStyle) { if (PlayerHelper.useSystemCaptionStyle) {
// set the subtitle style // set the subtitle style
val captionStyle = PlayerHelper.getCaptionStyle(requireContext()) val captionStyle = PlayerHelper.getCaptionStyle(requireContext())
exoPlayerView.subtitleView?.setApplyEmbeddedStyles(captionStyle == CaptionStyleCompat.DEFAULT) exoPlayerView.subtitleView?.setApplyEmbeddedStyles(captionStyle == CaptionStyleCompat.DEFAULT)
@ -943,7 +837,7 @@ class PlayerFragment : BaseFragment() {
// Listener for play and pause icon change // Listener for play and pause icon change
exoPlayer.addListener(object : Player.Listener { exoPlayer.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) { override fun onIsPlayingChanged(isPlaying: Boolean) {
if (isPlaying && sponsorBlockEnabled) { if (isPlaying && PlayerHelper.sponsorBlockEnabled) {
Handler(Looper.getMainLooper()).postDelayed( Handler(Looper.getMainLooper()).postDelayed(
this@PlayerFragment::checkForSegments, this@PlayerFragment::checkForSegments,
100 100
@ -1049,7 +943,7 @@ class PlayerFragment : BaseFragment() {
} }
} }
} }
if (relatedStreamsEnabled) { if (PlayerHelper.relatedStreamsEnabled) {
// only show related streams if enabled // only show related streams if enabled
binding.relatedRecView.adapter = TrendingAdapter( binding.relatedRecView.adapter = TrendingAdapter(
response.relatedStreams!!, response.relatedStreams!!,
@ -1099,13 +993,13 @@ class PlayerFragment : BaseFragment() {
// next and previous buttons // next and previous buttons
playerBinding.skipPrev.visibility = if ( playerBinding.skipPrev.visibility = if (
skipButtonsEnabled && PlayingQueue.hasPrev() PlayerHelper.skipButtonsEnabled && PlayingQueue.hasPrev()
) { ) {
View.VISIBLE View.VISIBLE
} else { } else {
View.INVISIBLE View.INVISIBLE
} }
playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE playerBinding.skipNext.visibility = if (PlayerHelper.skipButtonsEnabled) View.VISIBLE else View.INVISIBLE
playerBinding.skipPrev.setOnClickListener { playerBinding.skipPrev.setOnClickListener {
videoId = PlayingQueue.getPrev() videoId = PlayingQueue.getPrev()
@ -1199,9 +1093,9 @@ class PlayerFragment : BaseFragment() {
videoUri: Uri, videoUri: Uri,
audioUrl: String audioUrl: String
) { ) {
val checkIntervalSize = when (progressiveLoadingIntervalSize) { val checkIntervalSize = when (PlayerHelper.progressiveLoadingIntervalSize) {
"default" -> ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES "default" -> ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES
else -> progressiveLoadingIntervalSize.toInt() * 1024 else -> PlayerHelper.progressiveLoadingIntervalSize.toInt() * 1024
} }
val dataSourceFactory: DataSource.Factory = val dataSourceFactory: DataSource.Factory =
@ -1249,7 +1143,7 @@ class PlayerFragment : BaseFragment() {
for (vid in streams.videoStreams!!) { for (vid in streams.videoStreams!!) {
// append quality to list if it has the preferred format (e.g. MPEG) // append quality to list if it has the preferred format (e.g. MPEG)
val preferredMimeType = "video/$videoFormatPreference" val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}"
if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format
videosNameArray += vid.quality.toString() videosNameArray += vid.quality.toString()
videosUrlArray += vid.url!!.toUri() videosUrlArray += vid.url!!.toUri()
@ -1281,9 +1175,9 @@ class PlayerFragment : BaseFragment() {
} }
// set the default subtitle if available // set the default subtitle if available
if (defaultSubtitleCode != "" && subtitleCodesList.contains(defaultSubtitleCode)) { if (PlayerHelper.defaultSubtitleCode != "" && subtitleCodesList.contains(PlayerHelper.defaultSubtitleCode)) {
val newParams = trackSelector.buildUponParameters() val newParams = trackSelector.buildUponParameters()
.setPreferredTextLanguage(defaultSubtitleCode) .setPreferredTextLanguage(PlayerHelper.defaultSubtitleCode)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) .setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
trackSelector.setParameters(newParams) trackSelector.setParameters(newParams)
} }
@ -1301,10 +1195,11 @@ class PlayerFragment : BaseFragment() {
videosNameArray: Array<String>, videosNameArray: Array<String>,
videosUrlArray: Array<Uri> videosUrlArray: Array<Uri>
) { ) {
if (defRes != "") { val defaultResolution = PlayerHelper.getDefaultResolution(requireContext())
if (defaultResolution != "") {
videosNameArray.forEachIndexed { index, pipedStream -> videosNameArray.forEachIndexed { index, pipedStream ->
// search for quality preference in the available stream sources // search for quality preference in the available stream sources
if (pipedStream.contains(defRes)) { if (pipedStream.contains(defaultResolution)) {
val videoUri = videosUrlArray[index] val videoUri = videosUrlArray[index]
val audioUrl = val audioUrl =
PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!) PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
@ -1350,7 +1245,7 @@ class PlayerFragment : BaseFragment() {
.setBackBuffer(1000 * 60 * 3, true) .setBackBuffer(1000 * 60 * 3, true)
.setBufferDurationsMs( .setBufferDurationsMs(
1000 * 10, // exo default is 50s 1000 * 10, // exo default is 50s
bufferingGoal, PlayerHelper.bufferingGoal,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
) )
@ -1505,7 +1400,7 @@ class PlayerFragment : BaseFragment() {
} }
private fun shouldStartPiP(): Boolean { private fun shouldStartPiP(): Boolean {
if (!pipEnabled || if (!PlayerHelper.pipEnabled ||
exoPlayer.playbackState == PlaybackState.STATE_PAUSED || exoPlayer.playbackState == PlaybackState.STATE_PAUSED ||
videoShownInExternalPlayer videoShownInExternalPlayer
) { ) {
@ -1537,9 +1432,8 @@ class PlayerFragment : BaseFragment() {
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
if (autoRotationEnabled) { if (!PlayerHelper.autoRotationEnabled) return
val orientation = newConfig.orientation when (newConfig.orientation) {
when (orientation) {
// go to fullscreen mode // go to fullscreen mode
Configuration.ORIENTATION_LANDSCAPE -> setFullscreen() Configuration.ORIENTATION_LANDSCAPE -> setFullscreen()
// exit fullscreen if not landscape // exit fullscreen if not landscape
@ -1547,4 +1441,3 @@ class PlayerFragment : BaseFragment() {
} }
} }
} }
}

View File

@ -162,4 +162,114 @@ object PlayerHelper {
else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
} }
} }
val autoRotationEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.AUTO_FULLSCREEN,
false
)
val relatedStreamsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.RELATED_STREAMS,
true
)
val pausePlayerOnScreenOffEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.PAUSE_ON_SCREEN_OFF,
false
)
val watchPositionsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_POSITION_TOGGLE,
true
)
val watchHistoryEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_HISTORY_TOGGLE,
true
)
val useSystemCaptionStyle: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SYSTEM_CAPTION_STYLE,
true
)
val videoFormatPreference: String
get() = PreferenceHelper.getString(
PreferenceKeys.PLAYER_VIDEO_FORMAT,
"webm"
)
val bufferingGoal: Int
get() = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL,
"50"
).toInt() * 1000
val sponsorBlockEnabled: Boolean
get() = PreferenceHelper.getBoolean(
"sb_enabled_key",
true
)
val sponsorBlockNotifications: Boolean
get() = PreferenceHelper.getBoolean(
"sb_notifications_key",
true
)
val defaultSubtitleCode: String
get() {
val code = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE,
""
)
if (code.contains("-")) {
return code.split("-")[0]
}
return code
}
val skipButtonsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SKIP_BUTTONS,
false
)
val pipEnabled: Boolean get() = PreferenceHelper.getBoolean(
PreferenceKeys.PICTURE_IN_PICTURE,
true
)
val skipSegmentsManually: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SB_SKIP_MANUALLY,
false
)
val progressiveLoadingIntervalSize: String
get() = PreferenceHelper.getString(
PreferenceKeys.PROGRESSIVE_LOADING_INTERVAL_SIZE,
"64"
)
fun getDefaultResolution(context: Context): String {
return if (NetworkHelper.isNetworkMobile(context)) {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
""
)
} else {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
}
}
} }