android - 具有固定纵横比的 ExoPlayer 布局

标签 android exoplayer

我正在尝试在 Android 上使用 ExoPlayer 构建类似 YouTube 的 View 。我希望视频出现在顶部,然后是标题、说明等其他内容。我所有的视频都是 16:9,我想要 SimpleExoPlayerView从 16:9 布局开始。使用下面的布局,播放器在切换到正确的视频纵横比之前占据整个屏幕几秒钟:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:keepScreenOn="true">

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

  </com.google.android.exoplayer2.ui.SimpleExoPlayerView>

  <TextView
    android:id="@+id/text_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="VideoTitle"
    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

  <TextView
    android:id="@+id/text_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="description" />
</LinearLayout>

最佳答案

我们正在使用 bundle 的 AspectRatioFrameLayout ExpoPlayer附带的:

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ui/AspectRatioFrameLayout.html

这就是你如何使用它:

<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:resize_mode="fixed_width">

    <!-- Preview image -->
    <ImageView
        android:id="@+id/imageViewVideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:contentDescription="@string/img_content_training_video_preview"
        android:focusable="true"
        android:scaleType="centerCrop" />

    <!-- Play button -->
    <ImageView
        android:id="@+id/imageTrainingPreviewIcon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:contentDescription="@string/img_content_training_video_preview"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_play" />

    <!-- Video player -->
    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlack"
        android:visibility="gone" />

    <!-- ProgressBar -->
    <include layout="@layout/custom_video_loading_progressbar" />

</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>

然后在您的 Activity 或 Fragment 上,您必须设置纵横比:
aspectRatioFrameLayout.setAspectRatio(16f/9f)

您还可以使用 setResizeMode 在代码中设置调整大小模式.

关于android - 具有固定纵横比的 ExoPlayer 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48935423/

相关文章:

Android Instagram API : how to retrieve pictures

android - HLS 协议(protocol),ParserException : Failed to parse the playlist, 无法识别任何标签

android - 离线drm。使用 exoplayer 获取离线安全内容

java -> 无法找到 com.google.android.exoplayer :exoplayer-core:r2. 4.2

android - ExoPlayer 不播放 Kitkat 中原始文件夹中的视频

java - 安卓工作室 : View who is visible only in preview

java - 如何等待 Android runOnUiThread 完成?

java - 如何解决(上下文: this)

android - 在 Exoplayer 中一起播放不同的 MediaSource

android - 如何使用 card.io android SDK 在人类可读格式中获取完整的卡号?