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
This commit is contained in:
parent
352dfbbc9b
commit
a8d5841c7c
@ -2,6 +2,7 @@ package com.brentvatne.exoplayer
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
|
import androidx.media3.common.Format
|
||||||
import com.brentvatne.common.api.ResizeMode
|
import com.brentvatne.common.api.ResizeMode
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
|
||||||
@ -94,4 +95,12 @@ class AspectRatioFrameLayout(context: Context) : FrameLayout(context) {
|
|||||||
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -291,22 +291,7 @@ class ExoPlayerView(private val context: Context) :
|
|||||||
if (group.type == C.TRACK_TYPE_VIDEO && group.length > 0) {
|
if (group.type == C.TRACK_TYPE_VIDEO && group.length > 0) {
|
||||||
// get the first track of the group to identify aspect ratio
|
// get the first track of the group to identify aspect ratio
|
||||||
val format = group.getTrackFormat(0)
|
val format = group.getTrackFormat(0)
|
||||||
|
layout.updateAspectRatio(format)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -325,18 +310,16 @@ class ExoPlayerView(private val context: Context) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onVideoSizeChanged(videoSize: VideoSize) {
|
override fun onVideoSizeChanged(videoSize: VideoSize) {
|
||||||
val isInitialRatio = layout.videoAspectRatio == 0f
|
|
||||||
if (videoSize.height == 0 || videoSize.width == 0) {
|
if (videoSize.height == 0 || videoSize.width == 0) {
|
||||||
// When changing video track we receive an ghost state with height / width = 0
|
// When changing video track we receive an ghost state with height / width = 0
|
||||||
// No need to resize the view in that case
|
// No need to resize the view in that case
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
layout.videoAspectRatio =
|
// Here we use updateForCurrentTrackSelections to have a consistent behavior.
|
||||||
((videoSize.width * videoSize.pixelWidthHeightRatio) / videoSize.height)
|
// according to: https://github.com/androidx/media/issues/1207
|
||||||
|
// sometimes media3 send bad Video size information
|
||||||
// React native workaround for measuring and layout on initial load.
|
player?.let {
|
||||||
if (isInitialRatio) {
|
updateForCurrentTrackSelections(it.currentTracks)
|
||||||
post(measureAndLayout)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user