1. 程式人生 > >Android VideoView 自動播放與重播,點選暫停與繼續

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