在CardView中播放视频

时间:2014-12-20 18:26:25

标签: android android-video-player android-recyclerview android-videoview android-cardview

我在RecyclerView中几张牌。每张卡都有一个自定义视频视图。点击视频视频后,视频应该开始播放。但是在我的情况下,它只是显示蓝色边框,视频没有运行。代码如下,

custom_card_view.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="5dip"
    card_view:cardCornerRadius="4dp">


    <com.test.components.CustomVideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        />

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dip"

        />


</android.support.v7.widget.CardView>

CustomVideoView:

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.VideoView;

import com.test.src.R;

    /**
     * Created by Psych on 12/20/14.
     */
    public class CustomVideoView extends VideoView {

        MediaController mMediaController = null;
        private String mVideoUrl = null;

        public CustomVideoView(Context context) {
            super(context);
        }

        public CustomVideoView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        /**
         * Initialize all the basic elements here.
         */
        private void initialize() {
            mMediaController = new MediaController(getContext());
        }

        /**
         * Starts up the video.
         * Throws a null pointer exception if the URL is not set
         */
        public void startVideo() {

            // Return as is if there is no URL
            if (mVideoUrl == null) {
                throw new NullPointerException(getContext().getString(R.string.video_null_pointer_message));
            }

            mMediaController.setAnchorView(this);
            setMediaController(mMediaController);
            start();
        }

        public void setmVideoUrl(String mVideoUrl) {
            this.mVideoUrl = mVideoUrl;
            Uri vidUri = Uri.parse(mVideoUrl);
            setVideoURI(vidUri);
        }

        public String getmVideoUrl() {
            return mVideoUrl;
        }
    }

MainActivity:

public class MainActivity extends ActionBarActivity {

    RecyclerView.LayoutManager mLayoutManager = null;
    RecyclerView mCardsRecyclerView = null;
    RecyclerView.Adapter mAdapter = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initComponents();

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mCardsRecyclerView.setHasFixedSize(true);
        mCardsRecyclerView.setLayoutManager(mLayoutManager);
        mCardsRecyclerView.setAdapter(mAdapter);

    }

    /**
     * Initialize all ui components/widgets/elements
     */
    private void initComponents() {
        mCardsRecyclerView = (RecyclerView) findViewById(R.id.cardsListView);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new NewsFeedAdapter();
    }
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/AppTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardsListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/alertButton" />


</RelativeLayout>

我希望每张卡最初只显示缩略图。点击视频时,相关卡应该启动视频(不是全部)。视频启动时,VideoView应占据整个屏幕。我怎样才能做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 This从另一个stackOverflow问题回答,为我解决了。这是通过在VideoView中给出高度(或宽度或两者)的显式值。例如

<VideoView
   android:id="@+id/mVideoView"
   android:layout_width="match_parent"
   android:layout_height="300dp"
   android:paddingLeft="5dp"
   android:paddingRight="5dp"/>