respect orientation pref for downloaded videos

This commit is contained in:
Bnyro 2022-10-07 19:18:04 +02:00
parent 512844805a
commit 5df5a2a9c5
3 changed files with 29 additions and 22 deletions

View File

@ -18,6 +18,7 @@ import com.github.libretube.databinding.ActivityOfflinePlayerBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.util.DownloadHelper import com.github.libretube.util.DownloadHelper
import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
@ -48,6 +49,8 @@ class OfflinePlayerActivity : BaseActivity() {
initializePlayer() initializePlayer()
playVideo() playVideo()
requestedOrientation = PlayerHelper.getOrientation(player.videoSize)
} }
private fun initializePlayer() { private fun initializePlayer() {

View File

@ -154,7 +154,6 @@ class PlayerFragment : BaseFragment() {
private var relatedStreamsEnabled = true private var relatedStreamsEnabled = true
private var autoRotationEnabled = true private var autoRotationEnabled = true
private var pausePlayerOnScreenOffEnabled = false private var pausePlayerOnScreenOffEnabled = false
private var fullscreenOrientationPref = "ratio"
private var watchHistoryEnabled = true private var watchHistoryEnabled = true
private var watchPositionsEnabled = true private var watchPositionsEnabled = true
private var useSystemCaptionStyle = true private var useSystemCaptionStyle = true
@ -256,11 +255,6 @@ class PlayerFragment : BaseFragment() {
true true
) )
fullscreenOrientationPref = PreferenceHelper.getString(
PreferenceKeys.FULLSCREEN_ORIENTATION,
"ratio"
)
pausePlayerOnScreenOffEnabled = PreferenceHelper.getBoolean( pausePlayerOnScreenOffEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.PAUSE_ON_SCREEN_OFF, PreferenceKeys.PAUSE_ON_SCREEN_OFF,
false false
@ -584,22 +578,7 @@ class PlayerFragment : BaseFragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
if (!autoRotationEnabled) { if (!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 = when (fullscreenOrientationPref) { val orientation = PlayerHelper.getOrientation(exoPlayer.videoSize)
"ratio" -> {
val videoSize = exoPlayer.videoSize
// probably a youtube shorts video
if (videoSize.height > videoSize.width) {
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} // a video with normal aspect ratio
else {
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}
"auto" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
mainActivity.requestedOrientation = orientation mainActivity.requestedOrientation = orientation
} }

View File

@ -1,9 +1,11 @@
package com.github.libretube.util package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo
import android.view.accessibility.CaptioningManager import android.view.accessibility.CaptioningManager
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.video.VideoSize
object PlayerHelper { object PlayerHelper {
// get the audio source following the users preferences // get the audio source following the users preferences
@ -137,4 +139,27 @@ object PlayerHelper {
} }
return categories return categories
} }
fun getOrientation(videoSize: VideoSize): Int {
val fullscreenOrientationPref = PreferenceHelper.getString(
PreferenceKeys.FULLSCREEN_ORIENTATION,
"ratio"
)
return when (fullscreenOrientationPref) {
"ratio" -> {
// probably a youtube shorts video
if (videoSize.height > videoSize.width) {
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} // a video with normal aspect ratio
else {
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}
"auto" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}
} }