Android VideoView 自动播放与重播,点击暂停与继续

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //set no title bar 需要在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //如果上面的不起作用,可以换成下面的。
        if (getSupportActionBar()!=null) getSupportActionBar().hide();
        if (getActionBar()!=null) getActionBar().hide();
        //no status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.activity_edit_video_combine_preview);
        ButterKnife.bind(this);



        destVideoPath = getIntent().getExtras().getString(VIDEO_PATH);

        //设置文件路径并播放。
        videoView.setVideoPath(destVideoPath);
        videoView.start();

        //设置点击事件,OnClickListener不好用
        videoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(application, ""+videoView.isPlaying(), Toast.LENGTH_SHORT).show();
                if (videoView.isPlaying()){
                    videoView.pause();
                }else {
                    videoView.start();
                }
                return false;
            }
        });

        //设置循环播放
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });

    }

    public static void start(Context context, String destVideoPath) {
        Intent intent = new Intent(context, EditVideoCombinePreviewActivity.class);
        intent.putExtra(VIDEO_PATH, destVideoPath);
        context.startActivity(intent);
    }

你可能感兴趣的:(音视频)