Code example for VideoView

Methods: canSeekForwardgetCurrentPositiongetDuration, canPause, canSeekBackward, getBufferPercentage

0
        if (mPlayer == null) {
            return; 
        } 
         
        try { 
            if (mPauseButton != null && !mPlayer.canPause()) {
                mPauseButton.setEnabled(false);
            } 
            if (mRewButton != null && !mPlayer.canSeekBackward()) {
                mRewButton.setEnabled(false);
            } 
            if (mFfwdButton != null && !mPlayer.canSeekForward()) {
                mFfwdButton.setEnabled(false);
            } 
        } catch (IncompatibleClassChangeError ex) {
            // We were given an old version of the interface, that doesn't have 
            // the canPause/canSeekXYZ methods. This is OK, it just means we 
            // assume the media can be paused and seeked, and so we don't disable 
            // the buttons. 
        } 
    } 
     
    /** 
     * Show the controller on screen. It will go away 
     * automatically after 'timeout' milliseconds of inactivity. 
     * @param timeout The timeout in milliseconds. Use 0 to show 
     * the controller until hide() is called. 
     */ 
    public void show(int timeout) {
        if (!mShowing && mAnchor != null) {
            setProgress(); 
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            } 
            disableUnsupportedButtons(); 
 
            FrameLayout.LayoutParams tlp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM
            ); 
             
            mAnchor.addView(this, tlp);
            mShowing = true;
        } 
        updatePausePlay(); 
        updateFullScreen(); 
         
        // cause the progress bar to be updated even if mShowing 
        // was already true.  This happens, for example, if we're 
        // paused with the progress bar showing the user hits play. 
        mHandler.sendEmptyMessage(SHOW_PROGRESS);
 
        Message msg = mHandler.obtainMessage(FADE_OUT);
        if (timeout != 0) {
            mHandler.removeMessages(FADE_OUT);
            mHandler.sendMessageDelayed(msg, timeout);
        } 
    } 
     
    public boolean isShowing() { 
        return mShowing;
    } 
 
    /** 
     * Remove the controller from the screen. 
     */ 
    public void hide() { 
        if (mAnchor == null) {
            return; 
        } 
 
        try { 
            mAnchor.removeView(this);
            mHandler.removeMessages(SHOW_PROGRESS);
        } catch (IllegalArgumentException ex) {
            Log.w("MediaController", "already removed");
        } 
        mShowing = false;
    } 
 
    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;
 
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours   = totalSeconds / 3600;
 
        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else { 
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        } 
    } 
 
    private int setProgress() { 
        if (mPlayer == null || mDragging) {
            return 0; 
        } 
         
        int position = (int) mPlayer.getCurrentPosition();
        int duration = (int) mPlayer.getDuration();
        if (mProgress != null) {
            if (duration > 0) {
                // use long to avoid overflow 
                long pos = 1000L * position / duration;
                mProgress.setProgress( (int) pos);
            } 
            int percent = mPlayer.getBufferPercentage();
            mProgress.setSecondaryProgress(percent * 10);
        } 
 
        if (mEndTime != null)
            mEndTime.setText(stringForTime(duration));