【问题标题】:In Android, using exoplayer, how to fill surfaceview with a video that does not have the same aspect ratio with the device?在Android中,使用exoplayer,如何用与设备不具有相同纵横比的视频填充surfaceview?
【发布时间】:2023-09-07 05:41:01
【问题描述】:

我有一个使用 ExoPlayer 播放视频的活动。当我全屏时,除非设备的纵横比等于视频的纵横比,否则我会在视频的顶部和底部看到小黑条。

这是布局的样子:

<com.google.android.exoplayer.AspectRatioFrameLayout
    android:id="@+id/video_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true">

    <SurfaceView android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"/>

    <View android:id="@+id/shutter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"/>

</com.google.android.exoplayer.AspectRatioFrameLayout>

希望

aspectRatioFrameLayout.setAspectRatio(mVideo.getAspectRatio());

会解决问题,但我没有成功。 有没有办法让视频充满屏幕,即使部分视频从屏幕上被截断?

【问题讨论】:

  • 派对迟到了,在另一边:你如何确保SurfaceView + AspectRatioFrameLayout 保持正确的纵横比并尽可能地填充屏幕高度和/或宽度?你在SurfaceView 上匹配_parent 和...AspectRatioFrameLayout 上的什么?

标签: android video fullscreen exoplayer


【解决方案1】:

有一种最快的方法可以让视频充满屏幕。 转到SimpleExoPlayerView.java 类,将resizeModeAspectRatioFrameLayout.RESIZE_MODE_FIT 更改为AspectRatioFrameLayout.RESIZE_MODE_FILLRESIZE_MODE_FILL 将忽略指定的纵横比。 如果您在项目中使用 ExoPlayer 作为模块,请使用这种方式。

更新 如果你使用 ExoPlayer 作为库,他们有一个setResizeMode() 的方法。 您只需在视图中设置为AspectRatioFrameLayout.RESIZE_MODE_FILLsimpleExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL) 就是这样!

【讨论】:

  • simpleExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);它有助于用播放器填充父视图。
  • 如果我想设置纵横比怎么办
  • 如果您想保持视频的纵横比但将其扩展为完整视图大小,如果您的视频高于其宽度(纵向),则使用 fixed_width 或如果它的宽度大于其高度,则使用 fixed_height (风景)
  • 它已弃用,现在有什么解决方案?
【解决方案2】:

如果您使用SimpleExoPlayerView,这也可以在 XML 中完成。

只需添加app:resize_mode="zoom" 以保持视频的纵横比或app:resize_mode="fill" 如果您不关心纵横比。

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
            android:id="@+id/video"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:resize_mode="zoom" />

【讨论】:

  • resize_mode 标签也存在于com.google.android.exoplayer2.ui.PlayerView 上。迄今为止最简单的解决方案。 +1
【解决方案3】:

您可以删除com.google.android.exoplayer.AspectRatioFrameLayout

接下来,在match_parent 上放一个简单的FrameLayout,宽度和高度。

请注意,SurfaceView 应保持在宽度和高度 = match_parent

解释:AspectRatioFrameLayout 正在根据真实视频比例设置正确的视图高度(根据宽度、高度和像素比例计算)。如果您不想这样做,Exoplayer 仍然可以像以前一样在SurfaceView 上显示视图。如果SurfaceView 不是@Override,它将没有纵横比。

【讨论】:

  • 谢谢!抱歉回复晚了……成功了!如此简单,只需更改框架布局即可
【解决方案4】:

只需在您的 xml 中使用波纹管。它会完美运行。

app:resize_mode="fill"

【讨论】: