Skip to content

Commit

Permalink
fix: ensure aspect ratio from video is handled in a coherent way (The…
Browse files Browse the repository at this point in the history
…WidlarzGroup#4219)

* fix: ensure aspect ratio from video is handled in a coherent way
  • Loading branch information
freeboub authored Oct 12, 2024
1 parent 352dfbb commit a8d5841
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
}
29 changes: 6 additions & 23 deletions android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
}
}

Expand Down

0 comments on commit a8d5841

Please sign in to comment.