我想用视频显示在列表视图多个视频但不能够做到这一点 [英] I want to display multiple video in listview using video but not able to do this

查看:222
本文介绍了我想用视频显示在列表视图多个视频但不能够做到这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code。

public class MainActivity extends Activity {
    ListView list;
    public ArrayList<String> videoList;
    public CustomListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list =(ListView)findViewById(R.id.list);
        videoList = new ArrayList<String>();
        videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
        videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
        videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
        videoList.add("http://www.youtube.com/watch?v=DdlWPL53PvQ#sthash.fW5EtDFb.dpuf");
        Log.d("size of videoList:",""+ videoList.size());
        adapter = new CustomListAdapter(MainActivity.this, videoList);
        list.setAdapter(adapter);
    }
    public class CustomListAdapter extends BaseAdapter {
        private Activity activity;
        private LayoutInflater inflater;
        private ArrayList<String> videoList;

        public CustomListAdapter(Activity activity, ArrayList<String> videoList) {
            this.activity = activity;
            this.videoList = videoList;
        }

        @Override
        public int getCount() {
            return videoList.size();
        }

        @Override
        public Object getItem(int location) {
            return videoList.get(location);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (inflater == null)
                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null)
                convertView = inflater.inflate(R.layout.list_row, null);

              VideoView video = (VideoView)convertView.findViewById(R.id.video_view);
             video.setVideoPath(videoList.get(position));
             video.start();
             return convertView;
        }

    }    
}

activity_main.xml中

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

list_raw.xml

list_raw.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >

    <!-- Thumbnail Image -->


    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


</RelativeLayout>

我不能让视频和没有任何错误显示的logcat thenb是什么问题?
我添加Internet权限的清单是任何或许可或任何其他settting是必需的?

I can't get video and no any error display in logcat thenb what is the issue? I added Internet permission in manifest is any or permission or any other settting is required?

推荐答案

我觉得我们不应该把视频视图直接在列表中,因为在列表每一个观点都重绘或无效很多次,如果任何视频是被发挥和用户观看,他意外地滚动它关闭屏幕,Android操作系统将释放内存,因此再次将视频从开始播放。 Video.start()将被一次又一次地叫

I think we should not put video view directly in the list, Since each and every view in list are redrawn or invalidated lot of times and if any video is being played and watched by user and he accidentally scrolls it off the screen , Android OS will free up that memory and hence again video will be played from Start. Video.start() will be called again and again

最佳的解决方案是提供的任何视频帧的缩略图。 替换图像视图视频浏览,并在它的点击打开一个新的屏幕,把视频查看和播放给它。

Best Solution is to provide a thumbnail image of any frame of video . Replace Video view with image view and on click of it open a new screen where put a video view and play on to it.

这篇关于我想用视频显示在列表视图多个视频但不能够做到这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆