From a8d5841c7c0f9767ec095ffd8401b1579f32623f Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:51:57 +0200 Subject: [PATCH] fix: ensure aspect ratio from video is handled in a coherent way (#4219) * fix: ensure aspect ratio from video is handled in a coherent way --- .../exoplayer/AspectRatioFrameLayout.kt | 9 ++++++ .../com/brentvatne/exoplayer/ExoPlayerView.kt | 29 ++++--------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/AspectRatioFrameLayout.kt b/android/src/main/java/com/brentvatne/exoplayer/AspectRatioFrameLayout.kt index c26745fe..c6c108c8 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/AspectRatioFrameLayout.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/AspectRatioFrameLayout.kt @@ -2,6 +2,7 @@ package com.brentvatne.exoplayer import android.content.Context import android.widget.FrameLayout +import androidx.media3.common.Format import com.brentvatne.common.api.ResizeMode import kotlin.math.abs @@ -94,4 +95,12 @@ class AspectRatioFrameLayout(context: Context) : FrameLayout(context) { MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) ) } + + fun updateAspectRatio(format: Format) { + // There are weird cases when video height and width did not change with rotation so we need change aspect ration to fix it + when (format.rotationDegrees) { + 90, 270 -> videoAspectRatio = if (format.width == 0) 1f else (format.height * format.pixelWidthHeightRatio) / format.width + else -> videoAspectRatio = if (format.height == 0) 1f else (format.width * format.pixelWidthHeightRatio) / format.height + } + } } diff --git a/android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt b/android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt index a72381fc..98f2d0a5 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt @@ -291,22 +291,7 @@ class ExoPlayerView(private val context: Context) : if (group.type == C.TRACK_TYPE_VIDEO && group.length > 0) { // get the first track of the group to identify aspect ratio val format = group.getTrackFormat(0) - - // There are weird cases when video height and width did not change with rotation so we need change aspect ration to fix - layout.videoAspectRatio = when (format.rotationDegrees) { - // update aspect ratio ! - 90, 270 -> if (format.width == 0) { - 1f - } else { - (format.height * format.pixelWidthHeightRatio) / format.width - } - - else -> if (format.height == 0) { - 1f - } else { - (format.width * format.pixelWidthHeightRatio) / format.height - } - } + layout.updateAspectRatio(format) return } } @@ -325,18 +310,16 @@ class ExoPlayerView(private val context: Context) : } override fun onVideoSizeChanged(videoSize: VideoSize) { - val isInitialRatio = layout.videoAspectRatio == 0f if (videoSize.height == 0 || videoSize.width == 0) { // When changing video track we receive an ghost state with height / width = 0 // No need to resize the view in that case return } - layout.videoAspectRatio = - ((videoSize.width * videoSize.pixelWidthHeightRatio) / videoSize.height) - - // React native workaround for measuring and layout on initial load. - if (isInitialRatio) { - post(measureAndLayout) + // Here we use updateForCurrentTrackSelections to have a consistent behavior. + // according to: https://github.com/androidx/media/issues/1207 + // sometimes media3 send bad Video size information + player?.let { + updateForCurrentTrackSelections(it.currentTracks) } }