Fix video player auto rotation

This commit is contained in:
Matéo Duparc 2021-12-21 10:50:26 +01:00
parent be802aa5af
commit 57e93f0b49
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
2 changed files with 9 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.source.MediaSource import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.video.VideoSize
import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R import sushi.hardcore.droidfs.R
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
@ -18,7 +19,7 @@ abstract class MediaPlayer: FileViewerActivity() {
abstract fun bindPlayer(player: ExoPlayer) abstract fun bindPlayer(player: ExoPlayer)
protected open fun onPlaylistIndexChanged() {} protected open fun onPlaylistIndexChanged() {}
protected open fun onPlayerReady() {} protected open fun onVideoSizeChanged(width: Int, height: Int) {}
private fun createMediaSource(filePath: String): MediaSource { private fun createMediaSource(filePath: String): MediaSource {
val dataSourceFactory = GocryptfsDataSource.Factory(gocryptfsVolume, filePath) val dataSourceFactory = GocryptfsDataSource.Factory(gocryptfsVolume, filePath)
@ -37,10 +38,8 @@ abstract class MediaPlayer: FileViewerActivity() {
player.seekToDefaultPosition(currentPlaylistIndex) player.seekToDefaultPosition(currentPlaylistIndex)
player.playWhenReady = true player.playWhenReady = true
player.addListener(object : Player.Listener{ player.addListener(object : Player.Listener{
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) { override fun onVideoSizeChanged(videoSize: VideoSize) {
if (playbackState == Player.STATE_READY) { onVideoSizeChanged(videoSize.width, videoSize.height)
onPlayerReady()
}
} }
override fun onPlayerError(error: PlaybackException) { override fun onPlayerError(error: PlaybackException) {
CustomAlertDialogBuilder(this@MediaPlayer, themeValue) CustomAlertDialogBuilder(this@MediaPlayer, themeValue)

View File

@ -36,9 +36,12 @@ class VideoPlayer: MediaPlayer() {
return "video" return "video"
} }
override fun onPlayerReady() { override fun onVideoSizeChanged(width: Int, height: Int) {
if (firstPlay && autoFit) { if (firstPlay && autoFit) {
requestedOrientation = if (binding.videoPlayer.width < binding.videoPlayer.height) ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE requestedOrientation = if (width < height)
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
else
ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
firstPlay = false firstPlay = false
} }
} }