• 由VideoView(全屏)+ImageView组成ViewPager的Item,绑定至Fragment;
  • 将Fragment装入FragmentStatePagerAdapter
  • 将adapter装载至viewPager;
  • 放入适量视频文件、图片素材等佐料后起锅...(好像又跑偏了啊喂),对于viewpager fragment这些基本组件,大家应该信手拈来了,我就说说视频文件如何播放的,翻开fragment,来看看每个item都有什么(敲黑板,这个是重点):
  • 布局:

    <?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="fill_parent">  
        <com.watire.xiamivd.FullScreenVideoView
            android:id="@+id/vvSplash"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <ImageView
            android:id="@+id/ivSlogan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:src="@drawable/slogan_1"
            android:scaleType="fitEnd"
            android:layout_alignParentEnd="true" />  
    </RelativeLayout>

    fragment:

        mVideoView = findViewById(R.id.vvSplash);
        mvSlogan = findViewById(R.id.ivSlogan);
        mVideoView.setOnErrorListener(this);
        mVideoView.setOnPreparedListener(this);
        mVideoView.setVideoPath("android.resource://" + getActivity().getPackageName() + "/" + R.raw.xxx);
        mvSlogan.setImageResource(imgRes);

    给videoView setVideoPath即可设置视频路径,此处加载raw文件夹中资源,实现MediaPlayer.OnPreparedListener进行播放。

        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            if (mVideoView != null) {
                mVideoView.requestFocus();
                mVideoView.setOnCompletionListener(this);
                mVideoView.seekTo(0);
                mVideoView.start();
            return;
                        

    然后实现MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener来处理播放完成(控制viewpager跳转至下一页或已是最后一页,则关闭页面)和播放失败时的情况。

        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            FragmentActivity localFragmentActivity = getActivity();
            if ((localFragmentActivity != null) && ((localFragmentActivity instanceof FullscreenActivity))) {
                ((FullscreenActivity) localFragmentActivity).next(position);
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            FragmentActivity localFragmentActivity = getActivity();
            if ((localFragmentActivity != null) && ((localFragmentActivity instanceof FullscreenActivity))) {
                ((FullscreenActivity) localFragmentActivity).next(position);
            return true;
                        

    另外,需要实现onPause() 和onResume(),在页面中断时停止播放、恢复时继续播放:

        public void onResume() {
            super.onResume();
            if (mHasPaused) {
                if (mVideoView != null) {
                    mVideoView.seekTo(mVideoPosition);
                    mVideoView.resume();
            return;
        public void onPause() {
            super.onPause();
            if (mVideoView != null) {
                mVideoPosition = mVideoView.getCurrentPosition();
            mHasPaused = true;
                        

    在onDestroy()时停止播放(敲黑板,这个必考啊):

        public void onDestroy() {
            super.onDestroy();
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            return;
                        

    重点就是这些了.
    源码请前往github-demo!

    ps. 推荐个github demo 运行神器:dryrun ,食用方法:

  • 连接手机;
  • 执行命令:dryrun github.com/watire/xiam…
  • 等待下载、安装。
  • 是不是很简单呢,当然要先安装dryrun~

    更多文章请关注公众号