Skip to content

Instantly share code, notes, and snippets.

@keima
Created March 23, 2018 14:19
Show Gist options
  • Save keima/49c64bfde1338d0e20a03f81ec370f0b to your computer and use it in GitHub Desktop.
Save keima/49c64bfde1338d0e20a03f81ec370f0b to your computer and use it in GitHub Desktop.
ExoPlayer + GLTextureView
import android.content.Context
import android.graphics.Matrix
import android.util.AttributeSet
import com.google.android.apps.muzei.render.GLTextureView
import com.google.android.exoplayer2.SimpleExoPlayer
import timber.log.Timber
class GLVideoTextureView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : GLTextureView(context, attrs), SimpleExoPlayer.VideoListener {
init {
setEGLContextClientVersion(2)
}
override fun onRenderedFirstFrame() {
// do nothing
}
override fun onVideoSizeChanged(width: Int, height: Int, unappliedRotationDegrees: Int, pixelWidthHeightRatio: Float) {
// Android 4.4なSO-03Fなどでは、portraitで撮影した映像とExoPlayerを介してレンダリングされる映像の回転が一致しないので対策する
// @see: https://github.com/google/ExoPlayer/issues/91#issuecomment-158106485
Timber.d("onVideoSizeChanged: $width, $height, $unappliedRotationDegrees, $pixelWidthHeightRatio")
val viewWidth = getWidth()
val viewHeight = getHeight()
val pivotX = viewWidth / 2F
val pivotY = viewHeight / 2F
setTransform(Matrix().apply {
postRotate(unappliedRotationDegrees.toFloat(), pivotX, pivotY)
if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) {
val viewAspectRatio = viewHeight / viewWidth.toFloat()
postScale(1 / viewAspectRatio, viewAspectRatio, pivotX, pivotY)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment