Android prevent VideoView from resizing on load video

时间:2019-01-07 13:28:11

标签: android android-videoview

I have an application that shows a lists of live streams. I'm using a VideoView in a RecyclerView to do this. However, it seems that the VideoView keeps on resizing which I think is probably based on the content. Here's my XML.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <VideoView
                android:id="@+id/vv_monitor"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_centerInParent="true" />

        </RelativeLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/text_view_margin"
            app:layout_constraintRight_toRightOf="parent">

            <TextView
                android:id="@+id/tv_watching"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/rounded_corner"
                android:drawableStart="@drawable/ic_remove_red_eye_white_12dp"
                android:drawablePadding="@dimen/text_view_margin"
                android:paddingLeft="@dimen/text_view_margin"
                android:paddingRight="@dimen/text_view_margin"
                android:text="@string/app_name"
                android:textColor="@android:color/white"
                android:textSize="@dimen/small_text_size" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:textSize="@dimen/small_text_size" />

            <TextView
                android:id="@+id/tv_status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textSize="@dimen/small_text_size" />

        </LinearLayout>

        <TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/small_text_size"
            tools:text="asdasda | h264 | mp4" />

    </LinearLayout>

</LinearLayout>

Here's where I call the VideoView in the adapter.

        viewHolder.videoViewMonitor.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
            }
        });
        viewHolder.videoViewMonitor.setVideoURI(Uri.parse(monitor.getUrl()));
        viewHolder.videoViewMonitor.start();

As shown in the code above, I set the height for my VideoView to 250dp. Initially when the Activity shows, the heights of the VideoViews in the RecyclerView are all equal. But when the content starts to load on some, the VideoView resizes and the height becomes smaller in size.

Calling the mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); doesn't seem to do anything.

Appreciate any feedback.

0 个答案:

没有答案