1

我使用 sdcard 路径位置在 recyclerview 中使用 cardview 完成了视频播放。还要检查 sdcard 不可用,然后从内部存储器中获取录制的视频。它在 Lava iris x8 设备中完美运行,但在三星 J5 和三星 Note 2 中,只有奇数(1、3、5、7)视频正在播放,而不是偶数(2、4、6、8)视频正在播放并获得类似对话滚动并获取偶数(2,4,6,8)个视频的位置时“无法播放此视频”。我不知道是什么问题?下面是我关于在 android 中的 recyclerview 和 cardview 中显示和播放视频的完整代码。

display_video.xml

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


    <RelativeLayout
        android:id="@+id/mRelative_background"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="#6FC298">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp">

            <RelativeLayout
                android:id="@+id/mRelativeClick"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="left">
            <ImageView
                android:id="@+id/mImage_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/icon_back"></ImageView>
            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="My Challenge"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:textStyle="italic" />

            <ImageView
                android:id="@+id/mImageView_setting"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@mipmap/icon_setting"></ImageView>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/mRelative_circular_image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="65dp">

            <ImageView
                android:id="@+id/mImageView_color1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:visibility="invisible"
                android:layout_marginLeft="10dp"
                android:src="@mipmap/color1"/>

            <ImageView
                android:id="@+id/mImageView_color2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                android:layout_toRightOf="@+id/mImageView_color1"
                android:src="@mipmap/color2"/>

            <ImageView
                android:id="@+id/mImageView_color3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                android:layout_toRightOf="@+id/mImageView_color2"
                android:src="@mipmap/color3"/>

            <app.stakes.CircularImageView
                android:id="@+id/mcircularImage"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:layout_centerInParent="true"
                android:background="@mipmap/camera_icon" />

            <ImageView
                android:id="@+id/mImageView_color4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                android:layout_toRightOf="@+id/mcircularImage"
                android:src="@mipmap/color4"/>

            <ImageView
                android:id="@+id/mImageView_color5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                android:layout_toRightOf="@+id/mImageView_color4"
                android:src="@mipmap/color5"/>

            <ImageView
                android:id="@+id/mImageView_color6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:visibility="invisible"
                android:layout_toRightOf="@+id/mImageView_color5"
                android:src="@mipmap/color6"/>

        </RelativeLayout>

        <TextView
            android:id="@+id/mTextViewUsername_VideoList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/mRelative_circular_image"
            android:layout_centerInParent="true"
            android:layout_marginTop="25dp"
            android:text="Text"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="italic" />

        <ImageView
            android:id="@+id/mImageView_color_table"
            android:src="@mipmap/color_table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/mRelative_circular_image"
            android:layout_marginTop="25dp"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/mRelativeTab"
        android:layout_below="@+id/mRelative_background"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/mImageViewtab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/list"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <ImageView
            android:id="@+id/mImageViewtab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/view"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp" />

        <ImageView
            android:id="@+id/mImageViewtab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/showbook"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="40dp"
             />

    </RelativeLayout>

    <RelativeLayout
        android:layout_below="@+id/mRelativeTab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:scrollbars="vertical" />
    </RelativeLayout>
</RelativeLayout>

cardview1.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="5dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:background="#FFFFFF"
        android:id="@+id/mRelativeMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--<ImageView
            android:id="@+id/iconId"
            android:layout_width="300dp"
            android:layout_height="250dp"
            android:layout_centerHorizontal="true"/>-->

        <app.stakes.CircularImageView
            android:id="@+id/circleImageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@mipmap/camera_icon" />

        <TextView
            android:id="@+id/tvVersionName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_toRightOf="@+id/circleImageview"
            android:layout_marginLeft="5dp"
            android:text="Auther"
            android:textColor="@android:color/black"
            android:textSize="7sp" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_toRightOf="@+id/circleImageview"
            android:layout_marginLeft="70dp"
            android:textColor="@android:color/black"
            android:textSize="10sp" />

        <ImageView
            android:id="@+id/mImageview_clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_toLeftOf="@+id/tvSec"
            android:src="@mipmap/timing"/>
        <TextView
            android:id="@+id/tvSec"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="35dp"
            android:text="Lollipop"
            android:textColor="@android:color/black"
            android:textSize="5sp" />

        <FrameLayout
            android:id="@+id/video_frame"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/circleImageview">

            <com.sprylab.android.widget.TextureVideoView
                android:id="@+id/mvideoView"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_marginTop="10dp"
                android:layout_gravity="center_horizontal|center_vertical"
                 />

                <ImageView
                    android:id="@+id/mImageview_play"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:src="@mipmap/play" />

                    <ImageView
                        android:id="@+id/mImageView_fav"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/mImageview_play"
                        android:layout_gravity="left|center_vertical"
                        android:layout_marginLeft="40dp"
                        android:layout_marginTop="130dp"
                        android:src="@mipmap/bookmark_video" />


            <ImageView
                android:id="@+id/mImageView_share"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_below="@+id/mImageview_play"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="40dp"
                android:layout_marginTop="130dp"
                android:src="@mipmap/sharevideo" />
                <!--</RelativeLayout>-->
        </FrameLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

Display_Video1.java

public class Display_Video1 extends ActionBarActivity {
public RecyclerView recyclerView;
private ArrayList<FeddProperties> os_versions;
private Adapter mAdapter;
private Adapter fav_video_adapter;
CircularImageView mcircularImage;
static DisplayMetrics dm;
static MediaController media_Controller;
RelativeLayout mRelativeClick;
static boolean isViewWithCatalog;
RelativeLayout mRelative_background;
static File OldFile;
static String[] fileList = null;
static String[] fileListOld = null;
/*static String FILE_PATH = newFile.getAbsolutePath();*/
static String FILEPATH_OLD;
static String videoOldFilePath;
static String replaceOldString;
String MiME_TYPE = "video/mp4";
static String videoFilePath;
static String replaceStr;
static String[] fav_video_list = null;

CallbackManager callbackManager;
public static String facebook_user_id;
public static String username;
Boolean isSDPresent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.display_video);

    isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    if(isSDPresent) {
        OldFile = new File(Environment.getExternalStorageDirectory(), "/Stakes_Download");
        MEDIA_PATHOLD =  new String(OldFile.getAbsolutePath());
        FILEPATH_OLD = OldFile.getAbsolutePath();
    }
    else{
        OldFile = new File(this.getFilesDir(), "Stakes_Download");
        MEDIA_PATHOLD =  new String(OldFile.getAbsolutePath());
        FILEPATH_OLD = OldFile.getAbsolutePath();
    }


   recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
            os_versions = new ArrayList<FeddProperties>();
            if (fileList != null) {


                for (int i = 0; i < fileList.length; i++) {
                    if (fileList[i].length() == 0) {
                        ArrayList<Integer> alist = new ArrayList<Integer>();
                        alist.add(fileList.length);
                        Toast.makeText(getApplicationContext(), "File Zero: " + Arrays.toString(fileList), Toast.LENGTH_LONG).show();

                    }
                }
            }

            if (fileListOld != null) {


                for (int i = 0; i < fileListOld.length; i++) {
                    if (fileListOld[i].length() == 0) {
                        ArrayList<Integer> alist = new ArrayList<Integer>();
                        alist.add(fileListOld.length);
                        Toast.makeText(getApplicationContext(), "File Zero: " + Arrays.toString(fileListOld), Toast.LENGTH_LONG).show();

                    }
                }
            }

            recyclerView.setHasFixedSize(true);

            // ListView
            recyclerView.setLayoutManager(new LinearLayoutManager(Display_Video1.this));


            // create an Object for Adapter
            if (fileList != null) {
                mAdapter = new CardViewDataAdapter(Display_Video1.this, fileList);
                // set the adapter object to the Recyclerview
                recyclerView.setAdapter(mAdapter);
            } else {
        /*Toast.makeText(getApplicationContext(),"No File Exist",Toast.LENGTH_LONG).show();*/
            }

    updateSongList();
    updateSongListOld();
    initContrls();
}

public void updateSongList() {

    File videoFiles = new File(MEDIA_PATHOLD);
    Log.d("**Value of videoFiles**", videoFiles.toString());

    if (videoFiles.isDirectory()) {
        fileList = videoFiles.list();
    }
    if (fileList == null) {
        System.out.println("File doesnot exit");
        Toast.makeText(this, "There is no file", Toast.LENGTH_SHORT).show();
    } else {
        System.out.println("fileList****************" + fileList);
        for (int i = 0; i < fileList.length; i++) {
            Log.e("Video:" + i + " File name", fileList[i]);

        }
    }
}

public void updateSongListOld() {

    File videoFiles = new File(MEDIA_PATHOLD);
    Log.d("**Value of videoFiles**", videoFiles.toString());

    if (videoFiles.isDirectory()) {
        fileListOld = videoFiles.list();
    }
    if (fileListOld == null) {
        System.out.println("File doesnot exit");
        Toast.makeText(this, "There is no file", Toast.LENGTH_SHORT).show();
    } else {
        System.out.println("fileList****************" + fileListOld);
        for (int i = 0; i < fileListOld.length; i++) {
            Log.e("Video:" + i + " File name", fileListOld[i]);

        }
    }
}

private void initContrls() {
    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    os_versions = new ArrayList<FeddProperties>();
    if (fileList != null) {
        /*gridView.setAdapter(new ImageAdapter(this, fileList));*/


        for (int i = 0; i < fileList.length; i++) {
            if (fileList[i].length() == 0) {
                ArrayList<Integer> alist = new ArrayList<Integer>();
                alist.add(fileList.length);
                Toast.makeText(getApplicationContext(), "File Zero: " + Arrays.toString(fileList), Toast.LENGTH_LONG).show();

               /* FeddProperties feed = new FeddProperties();

                feed.setTitle(fileList[i]);
                *//*feed.setThumbnail(icons[i]);*//*
                os_versions.add(feed);*/
            }
        }
    }

    if (fileListOld != null) {
        /*gridView.setAdapter(new ImageAdapter(this, fileList));*/


        for (int i = 0; i < fileListOld.length; i++) {
            if (fileListOld[i].length() == 0) {
                ArrayList<Integer> alist = new ArrayList<Integer>();
                alist.add(fileListOld.length);
                Toast.makeText(getApplicationContext(), "File Zero: " + Arrays.toString(fileListOld), Toast.LENGTH_LONG).show();

               /* FeddProperties feed = new FeddProperties();

                feed.setTitle(fileList[i]);
                *//*feed.setThumbnail(icons[i]);*//*
                os_versions.add(feed);*/
            }
        }
    }

    recyclerView.setHasFixedSize(true);

    // ListView
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //Grid View
    /* recyclerView.setLayoutManager(new GridLayoutManager(this,2,1,false));*/

    //StaggeredGridView
     /*recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));*/

    // create an Object for Adapter
    if (fileList != null) {
        mAdapter = new CardViewDataAdapter(Display_Video1.this, fileList);
        // set the adapter object to the Recyclerview
        recyclerView.setAdapter(mAdapter);
    } else {
        /*Toast.makeText(getApplicationContext(),"No File Exist",Toast.LENGTH_LONG).show();*/
    }


    recyclerView.addOnItemTouchListener(new CardViewDataAdapter(Display_Video1.this, new CardViewDataAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

        }
    }));
}

Recyclerview 适配器

public static class CardViewDataAdapter extends Adapter<CardViewDataAdapter.ViewHolder> implements OnItemTouchListener {
        private ArrayList<FeddProperties> dataSet;
        private OnItemClickListener mListener;
        GestureDetector mGestureDetector;
        private Context context;
        private String[] VideoValues;
        private int position = 0;
        public interface OnItemClickListener {
            public void onItemClick(View view, int position);
        }
        public CardViewDataAdapter(Context context, String[] VideoValues) {

            /*dataSet = os_versions;*/
            this.context = context;
            this.VideoValues = VideoValues;
        }
        public CardViewDataAdapter(Context context, OnItemClickListener listener) {

            mListener = listener;
            mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }
            });
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            // create a new view
            View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                    R.layout.card_view1, null);

            ViewHolder viewHolder = new ViewHolder(itemLayoutView);
            return viewHolder;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
            videoFilePath = FILEPATH_OLD + "/" + fileList[i];
            Toast.makeText(context,"Path: "+videoFilePath,Toast.LENGTH_LONG).show();
            viewHolder.tvVersionName.setText(fileList[i]);
            System.out
                    .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> file path>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                            + fileList[i]);

            viewHolder.mvideoView.setVideoPath(videoFilePath);
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(context, Uri.parse(videoFilePath));
            final String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            long timeInMillisec = Long.parseLong(time);
            final long duration = timeInMillisec / 1000;
            long hours = duration / 3600;
            long minutes = (duration - hours * 3600) / 60;
            final long seconds = duration - (hours * 3600 + minutes * 60);
            viewHolder.tvSec.setText(String.valueOf(seconds) + "s");

            viewHolder.mImageview_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewHolder.mvideoView!= null) {



                        if (viewHolder.mvideoView.isPlaying()) {
                            viewHolder.mvideoView.pause();
                            viewHolder.mImageview_play.setImageResource(R.mipmap.play);

                        } else {
                            viewHolder.mvideoView.start();
                            viewHolder.mImageview_play.setImageResource(R.mipmap.play_click);
                        }

                    }
                    viewHolder.mvideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            viewHolder.mImageview_play.setImageResource(R.mipmap.play);
                        }
                    });
                }
            });


            viewHolder.str = fileList;
        }

        @Override
        public int getItemCount() {
            return VideoValues.length;
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent e) {
            View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
                mListener.onItemClick(childView, recyclerView.getChildAdapterPosition(childView));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean b) {

        }

        // inner class to hold a reference to each item of RecyclerView
        public class ViewHolder extends RecyclerView.ViewHolder {

            public TextView tvVersionName;
            public TextView tvTitle;
            public TextView tvSec;
            /*public ImageView iconView;*/
            /*public RelativeLayout mvideoView;*/
            public TextureVideoView mvideoView;
            public ImageView mImageview_play;
            public CircularImageView circleImageview;
            public ImageView mImageView_fav;
            public ImageView mImageView_share;


            public FeddProperties feed;
            public String[] str;

            public ViewHolder(View itemLayoutView) {
                super(itemLayoutView);

                tvVersionName = (TextView) itemLayoutView
                        .findViewById(R.id.tvVersionName);
                /*iconView = (ImageView) itemLayoutView
                        .findViewById(R.id.iconId);*/

                tvSec = (TextView) itemLayoutView.findViewById(R.id.tvSec);
                tvTitle = (TextView) itemLayoutView.findViewById(R.id.tvTitle);
                mvideoView = (TextureVideoView) itemLayoutView.findViewById(R.id.mvideoView);

                mImageview_play = (ImageView) itemLayoutView.findViewById(R.id.mImageview_play);

            }

        }
    }
4

0 回答 0