使用VideoView实现视频轮番播放

版权声明:本文为博主收集的资料,欢迎参考。未经本人允许,禁止转载 https://blog.csdn.net/z302766296/article/details/51263722

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近项目中需要集成一个轮番播放广告的需求,因为是新手所以第一个当然会想到VideoView控件,所以这个需求也直接用VideoView实现了。</span>

当然,也可以使用SurfaceView+MediaPlayer来实现,测试效果之后,感觉SurfaceView播放视频的加载速度会比VideoView快。当然啦因为VideoView是SurfaceView的扩展

小新一枚,求望大神指导。希望能在Blog里面做做笔记也可以

先上个XML代码,很简单的两个东西。就一个VideoView,SurfaceView

<?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:fitsSystemWindows="true"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

然后就是MainActivity的代码了,鉴于这个功能实现起来其实也是很简单的,所以注释也很少

public class MainActivity extends BaseActivity {

    private VideoView mVideo;
    private static final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FwDoor/Video/";
    private int currentProgress;    //当前进度
    private String[] mVideoPaths;<span style="white-space:pre">	</span>//视频路径
    private int currentVideo = 0;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initViews() {

        mVideoPaths = new String[]{path + "0.mp4", path + "1.mp4", path + "2.mp4", path + "3.mp4"};

        mVideo = (VideoView) findViewById(R.id.video);
    }

    @Override
    public void initListeners() {
        mVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //标记播放
                currentVideo++;
                if (currentVideo == mVideoPaths.length) {
                    currentVideo = 0;
                }
                startVideoView(mVideoPaths[currentVideo]);
            }
        });
      
        
        mVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
    }

    @Override
    public void initData() {
        startVideoView(mVideoPaths[currentVideo]);
    }

    /**
     * 播放视频
     *
     * @param path 播放视频路径
     */
    public void startVideoView(String path) {
        Uri uri = Uri.parse(path);
        mVideo.setVideoURI(uri);
        mVideo.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mVideo != null) {
            mVideo.seekTo(currentProgress);
            mVideo.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mVideo.isPlaying()) {
            mVideo.pause();
            currentProgress = mVideo.getCurrentPosition();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideo != null) {
            mVideo = null;
        }
    }
}

当然也能用SurfaceView实现了,结合MediaPlayer.

通过SurfaceView获取SurfaceHolder.然后在SurfaceHolder.addCallBack方法中的surfaceCreated()方法中将MediaPlayer和Holder绑定即可

MediaPlayer.setDisplay(holder);






猜你喜欢

转载自blog.csdn.net/z302766296/article/details/51263722