Search code examples
androidkotlinexoplayer2.x

ExoPlayer resize works only first time its called


As mentioned above I'm having a problem with changing the resize mode on my ExoPlayer. I'm playing a list of videos that change on scroll and depending on the screen orientation I'm trying to make the video zoom in if its close to the screen AspectRatio, the block of code that handles that is:

  override fun onVideoSizeChanged(width: Int, height: Int, unappliedRotationDegrees: Int, pixelWidthHeightRatio: Float) {
    super.onVideoSizeChanged(width, height, unappliedRotationDegrees, pixelWidthHeightRatio)
    val displayMetrics = DisplayMetrics()
    (context as Activity).windowManager.defaultDisplay.getMetrics(displayMetrics)

    val displayAspectRatio = displayMetrics.widthPixels.toFloat() / displayMetrics.heightPixels
    val videoAspectRatio = width.toFloat() / height

    if (abs(videoAspectRatio - displayAspectRatio) < 0.2) {
      exoPlayerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
      selectedPlayer?.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
    } else {
      exoPlayerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
      selectedPlayer?.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT
    }

  }

But the exoPlayerView.resizeMode is visible only the first time this is called. Is there some other way to do this? Or maybe force the view to re-render?

Thanks in advance!


Solution

  • You can access size of your video with : exoplayer.videoFormat.width and exoplayer.videoFormat.height, so that whenever your video is starting you start your function to calculate the ratio and do your stuff