How to play videos one by one and play / pause while browsing the scroll list

I want to play a video that is on the frontView, how to detect the view in listView

and play when getView

and pause when scrolling up. How to do it?

This is my code.

public class MainActivity extends Activity {
    ListView listview;
    CustomListAdapter adapter;
    public static int currentItem;
    public static ArrayList<String> videoLIst;
    public static MediaController mediaController ;
    boolean pauseOnScroll = true;
    boolean pauseOnFling = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        videoLIst = new ArrayList<String>();
         mediaController = new MediaController(MainActivity.this); 
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        listview = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(MainActivity.this, videoLIst);
        // Set video link (mp4 format )
        listview.setAdapter(adapter);
        listview.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

                int firstVisibleRow = listview.getFirstVisiblePosition();
                int lastVisibleRow = listview.getLastVisiblePosition();

                for(int i=firstVisibleRow;i<=lastVisibleRow;i++)
                {
                    //Write your code here(allocation/deallocation/store in array etc.)
                    System.out.println(i + "=" + listview.getItemAtPosition(i));
                    currentItem = (int) listview.getItemAtPosition(i);
                }
            }
        });


    }

    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.activity_main, null);

            VideoView video = (VideoView)convertView.findViewById(R.id.VideoView);
            TextView  txvvideo =(TextView)convertView.findViewById(R.id.txvposition);

            txvvideo.setText("Video"+position);

            mediaController.setAnchorView(video);
            Log.d("video url inadapter:", ""+videoLIst.toString());
            Uri uri = Uri.parse(videoList.get(currentItem));
            video.setMediaController(mediaController);
            video.setVideoURI(uri);
            video.start();
            int pos;
            pos=video.getCurrentPosition();
//          if(pos == currentItem)
//          {
//              video.pause();
//          }
//          video.setMediaController(mediaController);
//          video.setVideoURI(Uri.parse(videoList.get(position)));
//          video.start();
             return convertView;
        }

    }    

}

      

I want to stop the video when it is invisible in listView

when I scroll it up or down.

+3


source to share


3 answers


Create a RecyclerListener and set it to the ListView:

    listView.setRecyclerListener(new RecyclerListener() {
        @Override
        public void onMovedToScrapHeap(View view) {
            VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
            videoView.stopPlayback();
        }
    });

      



When the view scrolls offscreen and becomes "invisible", the ListView will recycle it, and you will receive a call to onMovedToScrapHeap ().

+1


source


I started with your code above and then made quite a few changes:

public class MainActivity extends Activity {
    ListView listview;
    CustomListAdapter adapter;
    public int mCurrentItem;
    public static ArrayList<String> sVideoList;
    static
    {
        sVideoList = new ArrayList<String>();
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
    }

    public static MediaController mediaController ;
    boolean pauseOnScroll = true;
    boolean pauseOnFling = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(MainActivity.this, sVideoList);
        // Set video link (mp4 format )
        listview.setAdapter(adapter);
        listview.setRecyclerListener(new AbsListView.RecyclerListener() {
            @Override
            public void onMovedToScrapHeap(View view) {
                // Safety net
                VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
                videoView.stopPlayback();
            }
        });
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int mFirstVisibleRow = -1;
            private int mActiveItem = -1;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                int firstVisibleRow = listview.getFirstVisiblePosition();
                if( mFirstVisibleRow != firstVisibleRow )
                {
                    mFirstVisibleRow = firstVisibleRow;

                    // Cancel the video of the previous active item
                    VideoView prevActiveVideoView = getVideoViewForRow(mActiveItem);
                    if( prevActiveVideoView != null )
                    {
                        prevActiveVideoView.pause();
                    }

                    // Start the video of the new active item
                    mActiveItem = mFirstVisibleRow + 1;
                    VideoView newActiveVideoView = getVideoViewForRow(mActiveItem);
                    if(newActiveVideoView != null)
                    {
                        newActiveVideoView.start();
                    }
                }
            }

            private VideoView getVideoViewForRow(int row)
            {
                int firstVisibleRow = listview.getFirstVisiblePosition();
                View rowView = listview.getChildAt(row-firstVisibleRow);
                return (rowView == null) ? null : (VideoView)rowView.findViewById(R.id.VideoView);
            }

        });

    }

    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.video_list_item, null);

            VideoView videoView = (VideoView)convertView.findViewById(R.id.VideoView);
            TextView videoTextView = (TextView)convertView.findViewById(R.id.txvposition);

            // We setup the video here but we don't start it until the view is scrolled to
            videoTextView.setText("Video " + position);
            Uri uri = Uri.parse(videoList.get(position));
            videoView.setVideoURI(uri);
            return convertView;
        }

    }

}

      

And video_list_item.xml:



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

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

    <TextView
        android:id="@+id/txvposition"
        android:layout_width="match_parent"
        android:layout_height="80dp" />

</LinearLayout>

      

Instead of starting the video when the view is created, you wait for it until it becomes active. Only one VideoView is allowed to activate at a time and when it stops being active, it pauses. It is currently coded so that the second visible VideoView on the screen is active.

This is not perfect code. that is, since it is currently implemented, the first VideoView in the listview will never play its video. However, it solves the problem above and helps you keep iterating.

0


source


there are two problems with this.

  • control video playback. (use textureView

    )
  • which is currently displayed in a scrollable list.

try this link and this

0


source







All Articles