From 57e93f0b49a5ba6e6a3e903d2cb86585b9c4893b Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 21 Dec 2021 10:50:26 +0100 Subject: [PATCH] Fix video player auto rotation --- .../sushi/hardcore/droidfs/file_viewers/MediaPlayer.kt | 9 ++++----- .../sushi/hardcore/droidfs/file_viewers/VideoPlayer.kt | 7 +++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/MediaPlayer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/MediaPlayer.kt index 4a2fdff..5d5d974 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/MediaPlayer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/MediaPlayer.kt @@ -5,6 +5,7 @@ import com.google.android.exoplayer2.* import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory import com.google.android.exoplayer2.source.MediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource +import com.google.android.exoplayer2.video.VideoSize import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.R import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder @@ -18,7 +19,7 @@ abstract class MediaPlayer: FileViewerActivity() { abstract fun bindPlayer(player: ExoPlayer) protected open fun onPlaylistIndexChanged() {} - protected open fun onPlayerReady() {} + protected open fun onVideoSizeChanged(width: Int, height: Int) {} private fun createMediaSource(filePath: String): MediaSource { val dataSourceFactory = GocryptfsDataSource.Factory(gocryptfsVolume, filePath) @@ -37,10 +38,8 @@ abstract class MediaPlayer: FileViewerActivity() { player.seekToDefaultPosition(currentPlaylistIndex) player.playWhenReady = true player.addListener(object : Player.Listener{ - override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) { - if (playbackState == Player.STATE_READY) { - onPlayerReady() - } + override fun onVideoSizeChanged(videoSize: VideoSize) { + onVideoSizeChanged(videoSize.width, videoSize.height) } override fun onPlayerError(error: PlaybackException) { CustomAlertDialogBuilder(this@MediaPlayer, themeValue) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/VideoPlayer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/VideoPlayer.kt index aa1c781..c66f80a 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/VideoPlayer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/VideoPlayer.kt @@ -36,9 +36,12 @@ class VideoPlayer: MediaPlayer() { return "video" } - override fun onPlayerReady() { + override fun onVideoSizeChanged(width: Int, height: Int) { 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 } }