当listview的列表项在屏幕上完全可见时,如何在视频视图中播放和暂停视频

时间:2016-12-07 09:46:33

标签: android listview android-videoview listviewitem

我已尝试过所有功能,但仍然无法获得准确的解决方案。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="400dp">
    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:visibility="visible" />
</LinearLayout>
</LinearLayout>

VideoPlayPause.Class

public class VideoPlayPause extends Activity {

ListView listView;
boolean aftrpause = false;
MediaController mediacontroller;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri videouri1 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny);
    Uri videouri3 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alien);
    Uri videouri4 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.concept);

    ArrayList<Uri> videouri = new ArrayList<Uri>();

    videouri.add(videouri1);
    videouri.add(videouri4);
    videouri.add(videouri3);
    videouri.add(videouri1);
    videouri.add(videouri4);

    listView = (ListView) findViewById(R.id.list);

    CustomVideoAdapter adapter = new CustomVideoAdapter(this,
            R.layout.activity_listview, videouri);
    listView.setAdapter(adapter);

}

public class CustomVideoAdapter extends ArrayAdapter<Uri> {

    Context context;
    ArrayList<Uri> items;

    public CustomVideoAdapter(Context context, int resourceId, ArrayList<Uri> videouri) {
        super(context, resourceId, videouri);
        this.context = context;
        this.items = videouri;

    }
    /*private view holder class*/
    private class ViewHolder {
        VideoView videoView;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        View v = convertView;

        if (v == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.activity_listview, null);
            holder = new ViewHolder();
            holder.videoView = (VideoView) convertView.findViewById(videoview);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        Log.i("UriSSSSSSSSSSSSSSS", "" + items.get(position));

            try {
                // Start the MediaController
                mediacontroller = new MediaController(context);
                mediacontroller.setVisibility(View.GONE);
                mediacontroller.setAnchorView(holder.videoView);
                holder.videoView.setMediaController(mediacontroller);

                Log.i("Uris out", items.get(position) + "");
                holder.videoView.setVideoURI(Uri.parse(String.valueOf(items.get(position))));

            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }


        final ViewHolder finalHolder = holder;

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            int time = 0, pausetime;
            Rect r = new Rect();
            boolean isViewVisible = finalHolder.videoView.isShown();
            View currentFocusedLayout, oldFocusedLayout;

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

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

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

                finalHolder.videoView.getGlobalVisibleRect(r);

                firstVisibleRow = listView.getFirstVisiblePosition();

                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));
                    Log.i("i Position>>>>", "" + i);
                    View listItem = listView.getChildAt(i);
                    if (listItem != null && listItem.isShown()) {
                        if (aftrpause) {
                            finalHolder.videoView.seekTo(pausetime);
                            finalHolder.videoView.start();
                        } else {
                            finalHolder.videoView.start();
                        }
                    } else {
                        pausetime = finalHolder.videoView.getCurrentPosition();
                        finalHolder.videoView.pause();
                        aftrpause = true;
                    }

                }

            }
        });
        holder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // setLooping(true) didn't work, thats why this workaround
                finalHolder.videoView.start();
            }
        });
        return convertView;
    }
  }
}

我也尝试了很多其他代码,但是没有人给我完美的解决方案,并且当我滚动listview时,视频位置也会随机变化。请尽可能帮助我

Thanx提前

1 个答案:

答案 0 :(得分:0)

您可以使用RecyclerView代替。并尝试此代码

RecyclerView rv_featureList;
    private View currentFocusedLayout, oldFocusedLayout;

    @Override
    public void onCreate(Bundle instanceState) {
        rv_featuredList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);


                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    //get the recyclerview position which is completely visible and first
                    int positionView = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                    Log.i("VISISBLE", positionView + "");
                    if (positionView >= 0) {
                        if (oldFocusedLayout != null) {
                            //Stop the previous video playback after new scroll

                        }
                        currentFocusedLayout = ((LinearLayoutManager) recyclerView.getLayoutManager()).findViewByPosition(positionView);

                        //play video
                        oldFocusedLayout = currentFocusedLayout;
                    }
                }

            }

        });
    }

以下是实施video playback in recyclerview

的教程