Exoplayer视频在加载完整版面之前全屏播放

时间:2018-02-18 19:56:17

标签: android android-relativelayout exoplayer

在我的Android应用程序中。我在详细活动中添加了exoplayer,可以通过单击回收站视图列表项中的链接来访问它。 现在问题是在加载详细信息屏幕时。

Exoplayer填满屏幕片刻,然后布局正在填充。 我希望在没有全屏加载的情况下单击列表中的项目后将视频加载到布局屏幕中。请在下面找到关于相同的GIF。

enter image description here

详细信息屏幕的XML布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.google.android.exoplayer2.ui.SimpleExoPlayerView
            android:id="@+id/exo_player_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/cardview_dark_background" />

        <ImageView
            android:id="@+id/exo_player_thumbnail_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />

        <android.support.v4.widget.ContentLoadingProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/exo_player_progress_bar"
            style="?android:progressBarStyle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:backgroundTint="@color/colorPrimary" />


    </FrameLayout>

    <TextView
        android:id="@+id/recipe_short_desc_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:text="short description"
        android:textSize="18dp"
        android:textStyle="bold" />

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

2 个答案:

答案 0 :(得分:0)

您需要手动设置SimpleExoPlayerView的维度,因为播放器在加载视频之前不知道视频的宽高比,这是异步的。

因此在onCreate中,您需要使用findViewById(int id)查找SimpleExoPlayerView,并根据SimpleExolayerView的父视图宽度和视频的宽高比设置LayoutProperies的高度。

答案 1 :(得分:0)

使用视频高度和宽度尝试以下代码。 宽高比为16:9的视频效果很好。

xml:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </android.support.constraint.ConstraintLayout>

java:

    ConstraintLayout viewLayout = findViewById(R.id.layout_player);

    ViewGroup.LayoutParams params = viewLayout.getLayoutParams();
    int videoHeight = 148;
    int videoWidth = 264;
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    params.height = (int) (((float) videoHeight / (float) videoWidth) * (float) screenWidth);
    viewLayout.setLayoutParams(params);
相关问题