Videoview在滚动时重新缓冲

时间:2016-07-30 10:06:34

标签: android android-recyclerview android-videoview

我正在VideoView内实施Recyclerview。在再次滚动VideoView重新缓冲区直到它没有启动时,当它开始播放时,它在小滚动时很好。因此,VideoView需要花费大量时间才能开始。

recyclerView.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
                        CustomVideoView vv_dashboard = (CustomVideoView) oldFocusedLayout.findViewById(R.id.videoplayer);
                        ProgressBar progressBar = (ProgressBar) oldFocusedLayout.findViewById(R.id.progressBar);
                        vv_dashboard.stopPlayback();
                        progressBar.setVisibility(View.GONE);
                    }
                    currentFocusedLayout = ((LinearLayoutManager) recyclerView.getLayoutManager()).findViewByPosition(positionView);
                    CustomVideoView vv_dashboard = (CustomVideoView) currentFocusedLayout.findViewById(R.id.videoplayer);
                    ProgressBar progressBar = (ProgressBar) currentFocusedLayout.findViewById(R.id.progressBar);
                    //to play video of selected recylerview, videosData is an array-list which is send to recyclerview adapter to fill the view. Here we getting that specific video which is displayed through recyclerview.
                    playVideo(urls.get(positionView), vv_dashboard, progressBar);
                    oldFocusedLayout = currentFocusedLayout;
                }
            }

        }

    });


 private void playVideo(Video video, final CustomVideoView emVideo, final ProgressBar buffering) {
    emVideo.setVideoURI(Uri.parse(video.getUrl()));
    buffering.setVisibility(View.VISIBLE);
    emVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
            buffering.setVisibility(View.GONE);
        }
    });
}

0 个答案:

没有答案