Code example for VideoView

Methods: getCurrentPositiongetDurationpauseseekTo

0
 
    // This updates the time bar display (if necessary). It is called by 
    // mProgressChecker and also from places where the time bar needs 
    // to be updated immediately. 
    private int setProgress() { 
        mVideoPosition = mVideoView.getCurrentPosition();
        // If the video position is smaller than the starting point of trimming, 
        // correct it. 
        if (mVideoPosition < mTrimStartTime) {
            mVideoView.seekTo(mTrimStartTime);
            mVideoPosition = mTrimStartTime;
        } 
        // If the position is bigger than the end point of trimming, show the 
        // replay button and pause. 
        if (mVideoPosition >= mTrimEndTime && mTrimEndTime > 0) {
            if (mVideoPosition > mTrimEndTime) {
                mVideoView.seekTo(mTrimEndTime);
                mVideoPosition = mTrimEndTime;
            } 
            mController.showEnded();
            mVideoView.pause();
        } 
 
        int duration = mVideoView.getDuration();
        if (duration > 0 && mTrimEndTime == 0) {
            mTrimEndTime = duration;
        } 
        mController.setTimes(mVideoPosition, duration, mTrimStartTime, mTrimEndTime);
        return mVideoPosition;
    } 
 
    private void playVideo() { 
        mVideoView.start();
        mController.showPlaying();
        setProgress(); 
    } 
 
    private void pauseVideo() { 
        mVideoView.pause();
        mController.showPaused();
    } 
 
 
    private boolean isModified() { 
        int delta = mTrimEndTime - mTrimStartTime;
 
        // Considering that we only trim at sync frame, we don't want to trim 
        // when the time interval is too short or too close to the origin. 
        if (delta < 100 || Math.abs(mVideoView.getDuration() - delta) < 100) {
            return false; 
        } else { 
            return true; 
        } 
    } 
 
    private void trimVideo() { 
 
        mDstFileInfo = SaveVideoFileUtils.getDstMp4FileInfo(TIME_STAMP_NAME,
                getContentResolver(), mUri, getString(R.string.folder_download));
        final File mSrcFile = new File(mSrcVideoPath);
 
        showProgressDialog(); 
 
        new Thread(new Runnable() {
            @Override 
            public void run() { 
                try { 
                    VideoUtils.startTrim(mSrcFile, mDstFileInfo.mFile, 
                            mTrimStartTime, mTrimEndTime); 
                    // Update the database for adding a new video file. 
                    SaveVideoFileUtils.insertContent(mDstFileInfo, 
                            getContentResolver(), mUri); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                    mHandler.post(new Runnable(){ 
                        @Override 
                        public void run(){ 
                            Toast.makeText(getApplicationContext(), 
                                getString(R.string.fail_trim), 
                                Toast.LENGTH_SHORT) 
                                .show(); 
                            if (mProgress != null) { 
                                mProgress.dismiss(); 
                                mProgress = null; 
                            } 
                        } 
                    }); 
                    return; 
                } 
                // After trimming is done, trigger the UI changed. 
                mHandler.post(new Runnable() { 
                    @Override 
                    public void run() { 
                        Toast.makeText(getApplicationContext(),
                            getString(R.string.save_into, mDstFileInfo.mFolderName),
                            Toast.LENGTH_SHORT)
                            .show(); 
                        // TODO: change trimming into a service to avoid 
                        // this progressDialog and add notification properly. 
                        if (mProgress != null) {
                            mProgress.dismiss();
                            mProgress = null;
                            // Show the result only when the activity not stopped. 
                            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(mDstFileInfo.mFile), "video/*");
                            intent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, false);
                            startActivity(intent);
                            finish();
                        } 
                    } 
                }); 
            } 
        }).start(); 
    } 
 
    private void showProgressDialog() { 
        // create a background thread to trim the video. 
        // and show the progress. 
        mProgress = new ProgressDialog(this);
        mProgress.setTitle(getString(R.string.trimming));
        mProgress.setMessage(getString(R.string.please_wait));
        // TODO: make this cancelable. 
        mProgress.setCancelable(false);
        mProgress.setCanceledOnTouchOutside(false);
        mProgress.show();
    } 
 
    @Override 
    public void onPlayPause() { 
        if (mVideoView.isPlaying()) {
            pauseVideo(); 
        } else { 
            playVideo(); 
        } 
    } 
 
    @Override 
    public void onSeekStart() { 
        pauseVideo(); 
    } 
 
    @Override 
    public void onSeekMove(int time) {
        mVideoView.seekTo(time);
    } 
 
    @Override 
    public void onSeekEnd(int time, int start, int end) {
        mVideoView.seekTo(time);
        mTrimStartTime = start;
        mTrimEndTime = end;
        setProgress(); 
        // Enable save if there's modifications 
        mSaveVideoTextView.setEnabled(isModified());