使用Swift创建自动播放视频滚动视图的最佳方法是什么?

时间:2016-01-11 12:35:18

标签: ios swift uiscrollview avplayer

我正在开发一款类似Vine的应用程序。

在这个应用程序中,我有一个视图控制器,其中滚动视图中显示了几个视频。我想创建与Vine,Facebook或Instagram相同的概念,当滚动视图中显示当前视频时“自动播放”。

我开始考虑它了。首先,用于显示视频的最佳播放器是什么?我需要一个“轻型”播放器,因为我必须在同一个控制器上显示多个具有无限滚动功能的播放器。

然后,这对我来说并不是很清楚,但这就是我“看到”工作的方式:

  1. 我有一个滚动视图,里面有几个视频播放器
  2. 我使用滚动视图功能检测到屏幕上当前可见的项目
  3. 检测到时,我会自动播放当前项目的视频
  4. 如果用户滚动,我会自动暂停上一个视频以启动下一个可见视频。
  5. 这是好方法吗?这样做有可能吗?我应该在scrollview中使用什么作为控制器来显示我的视频播放器?

    谢谢:-)!

1 个答案:

答案 0 :(得分:0)

private ListView listView = null;

View getArticleListView()a
{
    listView = new ListView(context);

    listView.setAdapter(new ListAdaptor(context, titleList, descriptionList, pubDateList, thumbnailList, contentList, pageNumber));

    listView.setRecyclerListener(new AbsListView.RecyclerListener() {
        @Override
        public void onMovedToScrapHeap(View view) {
            View firstView = listView.getChildAt(0);

            if (ListAdaptor.videoPlayer != null)
                ListAdaptor.videoPlayer.stopPlayback();
        }
    });

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        private int mFirstVisibleRow = 0;
        private int mActiveItem = 0;

        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {


                int firstVisibleRow = listView.getFirstVisiblePosition();
                if( mFirstVisibleRow != firstVisibleRow )
                {
                    mFirstVisibleRow = firstVisibleRow;


                    VideoView prevActiveVideoView = getVideoViewForRow(mActiveItem); // Cancel the video of the previous active item
                    if( prevActiveVideoView != null )
                    {
                        prevActiveVideoView.setVisibility(View.INVISIBLE);
                        ListAdaptor.imgCardLitImage.setVisibility(View.VISIBLE);
                        ListAdaptor.imageVideoOverLay.setVisibility(View.VISIBLE);
                        prevActiveVideoView.pause();
                    }

                    // Start the video of the new active item
                    if(viewHeight > (listView.getChildAt(0).getHeight())/2 ){
                        mActiveItem = mFirstVisibleRow + 1;
                    }
                    else {
                        mActiveItem = mFirstVisibleRow;
                    }

                    final VideoView newActiveVideoView = getVideoViewForRow(mActiveItem);
                    if(newActiveVideoView != null)
                    {

                        newActiveVideoView.setVisibility(View.VISIBLE);

                        newActiveVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                mediaPlayer.start();
                                        mediaPlayer.setVolume(0, 0);
                            }
                        });
                    }

                }

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            View v = listView.getChildAt(0);
            int top = (v == null) ? 0 : (listView.getPaddingTop() - v.getTop());
            viewHeight = top;

        }

        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);
        }


    });

    return listView;
}