具有固定宽高比的ExoPlayer布局

问题描述 投票:0回答:2

我正在尝试使用Android上的ExoPlayer构建类似YouTube的视图。我希望视频显示在顶部,然后是其他一些内容,如标题,描述等。我所有的视频都是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>
android exoplayer
2个回答
1
投票

由于SimpleExoPlayerView的高度为“wrap_content”,因此系统需要时间来测量正确的高度(如您所述,需要几秒钟)。我建议你根据实际宽度使用特定的高度值。例如,使用sw360的设备的高度应为203,使用sw400的设备的高度应为225,使用sw480的设备的高度应为270.通过这种方式,您还可以保持16:9的宽高比。


0
投票

我们没有使用SimpleExoPlayer(我们只是使用Surface代替),但我们将其包装在FixedAspectRatioFrameLayout中。

然后SimpleExoPlayer应该嵌套在FrameLayout中,宽度和高度设置为match_parent,它应该填充正确的空间。

如果有效,请告诉我。

[编辑]在固定的AspectRatioFrameLayout中:

mAspectRatioWidth = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioWidth, 4);
mAspectRatioHeight = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioHeight, 3);

如果说4和3,那就是你放16和9的地方。

© www.soinside.com 2019 - 2024. All rights reserved.