Code example for SeekBar

Methods: getMaxsetProgresssetSecondaryProgress, getWidth

0
    } 
	 
	@Override 
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		 
		int pos = ((int) (((float)percent / 100.f) * (float)timeline.getMax()));
		timeline.setSecondaryProgress(pos);
		startPlayProgressUpdater(); 
	} 
 
	/** 
	 * Called when the media player is finished playing the video. 
	 */ 
	@Override 
    public void onCompletion(MediaPlayer mediaPlayer) {
    	 
        Log.i(TAG, "onCompletion called for " + mediaPlayer.toString());
         
        started = false;
         
        if (onMediaPlayerStateChange != null)
			onMediaPlayerStateChange.onMediaPlayerStateChange(STATE_END, null);
		 
		if (!loopVideo)
			resetPlayer(); 
    } 
 
    @Override 
	public boolean onError(MediaPlayer mp, int what, int extra) {
    	 
		Log.i(TAG, "There has been an error with the MediaPlayer: ERROR " + what);
         
		if (onMediaPlayerStateChange != null)
			onMediaPlayerStateChange.onMediaPlayerStateChange(STATE_ERROR, "" + extra);
		 
		releaseMediaPlayer(); 
		 
		return true; 
	} 
 
	@Override 
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		 
		Log.i(TAG, "INFO Message: " + what);
		 
		return false; 
	} 
 
    /** 
     * Once the MediaPlayer is prepared this method is called.  The video 
     * begins playing and other variables are initialized 
     */ 
    @Override 
    public void onPrepared(MediaPlayer mediaplayer) {
         
        mIsVideoReadyToBePlayed = true;
         
		if (mMediaPlayer != null)
			prepareVideoPlayback(); 
    } 
 
    /** 
     * Called when the video size changes 
     */ 
	@Override 
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
         
        if (mIsVideoReadyToBePlayed)
			prepareVideoPlayback(width, height);
    } 
	 
	/** 
	 * Prepares to begin playing the video once the MediaPlayer is prepared. Updates 
	 * UI elements and adjusts the screen size if necessary. 
	 */ 
	private void prepareVideoPlayback() { 
 
        mWaiting = false;
         
		orgWidth = mMediaPlayer.getVideoWidth();
		orgHeight = mMediaPlayer.getVideoHeight();
		duration = mMediaPlayer.getDuration();
		 
        timeline.setMax(duration);
		 
		prepareVideoPlayback(orgWidth, orgHeight);
	} 
     
	/** 
	 * Prepares to begin playing the video once the MediaPlayer is prepared. Updates 
	 * UI elements and adjusts the screen size if necessary. 
	 *  
	 * @param width The width of the video to be played 
	 * @param height The height of the video to be played 
	 */ 
    private void prepareVideoPlayback(int width, int height) {
 
		int[] size = adjustedVideoSize(width, height);
		 
        holder.setFixedSize(size[0], size[1]);
 
        mMediaPlayer.setVolume((float)volume.getProgress()/100, (float)volume.getProgress()/100);
        mMediaPlayer.setLooping(loopVideo);
 
        configureView(); 
 
		startPlayProgressUpdater(); 
		 
		if (autoplay && isPaused)
			pausePlay(); 
    } 
     
    public void restartMediaPlayer() { 
    	releaseMediaPlayer(); 
    	prepMediaPlayer(); 
    } 
 
    /** 
     * Called when the MediaPlayer is released 
     */ 
    public void releaseMediaPlayer() { 
 
        mIsVideoReadyToBePlayed = false;
    	started = false;
    	 
        if (mMediaPlayer != null) {
        	 
        	Log.i(TAG, "Media Player RELEASED");
            mMediaPlayer.release();
            mMediaPlayer = null;
             
            if (onMediaPlayerStateChange != null)
				onMediaPlayerStateChange.onMediaPlayerStateChange(STATE_RELEASED, null);
        } 
         
        surface.setVisibility(View.GONE);
        container.setVisibility(View.VISIBLE);
         
        hideControls(); 
         
        mWaiting = false;
         
        configureView(); 
         
        videoContainer.setVisibility(View.GONE);
    } 
     
    /** 
     * Starts a streaming MediaPlayer in the case that there is no external storage 
     * to download a buffer file. 
     */ 
    private void startMediaPlayerStream() { 
    	 
    	Log.i(TAG, "startMediaPlayerStreaming");
 
		mIsVideoReadyToBePlayed = false;
    	isPaused = true;
         
        try { 
	             
            // initializes the media player 
    		mMediaPlayer = new MediaPlayer();
        	mMediaPlayer.setDataSource(path);
        	mMediaPlayer.setDisplay(holder);
        	mMediaPlayer.setLooping(loopVideo);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.setOnInfoListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
             
        } catch (IOException e) {
        	 
        	if (onMediaPlayerStateChange != null)
				onMediaPlayerStateChange.onMediaPlayerStateChange(STATE_ERROR, e.getMessage());
        	 
        	releaseMediaPlayer(); 
        }    
    } 
     
    /** 
     * Keeps track of the progress of the video file playing and 
     * the last time an action was made in order to hide the video 
     * control panel 
     */ 
    private void startPlayProgressUpdater() { 
    	 
    	if (mMediaPlayer != null) {
    		 
    		// updates current position 
    		curPosition = mMediaPlayer.getCurrentPosition();
			 
    		int knobPosition = keepKnobVisible(timeline.getMax(), timeline.getWidth(), 26);
    		 
    		// updates the progress bar keeping the knob in view 
    		if (curPosition < knobPosition) {
    			 
    			timeline.setProgress(knobPosition);
    			 
    		} else if (curPosition > (timeline.getMax() - knobPosition)) {
    			 
    			timeline.setProgress(timeline.getMax() - knobPosition);
    			 
    		} else { 
    			 
    			timeline.setProgress(curPosition);
    		} 
    		 
	    	//updates the timers 
	    	timers(curPosition);