Code example for MediaController.MediaPlayerControl

Methods: getBufferPercentage, getCurrentPosition, getDuration, isPlaying

0
	{ 
		if (playerControl == null || isDragging)
		{ 
			return 0; 
		} 
		int position = playerControl.getCurrentPosition();
		int duration = playerControl.getDuration();
		if (null != seekbar)
		{ 
			if (0 < duration)
			{ 
				seekbar.setProgress(getProgressPercentage(position, duration));
			} 
			int percent = playerControl.getBufferPercentage();
			seekbar.setSecondaryProgress(percent);
			Logger.v(">> setProgress() " + position + " / " + duration + ", buffering : " + percent);
		} 
 
		if (null != time)
		{ 
			time.setText(stringForTime(duration));
		} 
		if (null != time_current)
		{ 
			time_current.setText(stringForTime(position));
		} 
 
		return position;
	} 
 
	/** 
	 * 진행상태 퍼센티지 계산하기. 
	 *  
	 * @param currentDuration 
	 * @param totalDuration 
	 * */ 
	private int getProgressPercentage(long currentDuration, long totalDuration)
	{ 
		Double percentage = (double) 0;
 
		long currentSeconds = (int) (currentDuration / 1000);
		long totalSeconds = (int) (totalDuration / 1000);
 
		// calculating percentage 
		percentage = (((double) currentSeconds) / totalSeconds) * 100;
 
		// return percentage 
		return percentage.intValue();
	} 
 
	/** 
	 * 진행바의 progress 정보를 현재 위치값으로 변환하기. 
	 *  
	 * @param progress 
	 *            SeekBar 의 getProgress() 정보 
	 * @param totalDuration 
	 *            returns current duration in milliseconds 
	 * */ 
	private int progressToTimer(int progress, int totalDuration)
	{ 
		int currentDuration = 0;
		totalDuration = (int) (totalDuration / 1000);
		currentDuration = (int) ((((double) progress) / 100) * totalDuration);
 
		// return current duration in milliseconds 
		return currentDuration * 1000;
	} 
 
	/** 
	 * 시간표시처리하기. 
	 *  
	 * @since 1.0.0 
	 * @param timeMs 
	 * @return 시간문자열 
	 */ 
	private String stringForTime(int timeMs)
	{ 
		int totalSeconds = timeMs / 1000;
 
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		int hours = totalSeconds / 3600;
 
		formatBuilder.setLength(0);
		if (hours > 0)
		{ 
			return formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
		} 
		else 
		{ 
			return formatter.format("%02d:%02d", minutes, seconds).toString();
		} 
	} 
 
	@Override 
	public boolean onTouchEvent(MotionEvent event)
	{ 
		show(defaultShowTimeout);
		return true; 
	} 
 
	/** 
	 * 핸들러 메시지 처리 
	 *  
	 * @since 1.0.0 
	 * @param msg 
	 */ 
	public void handleMessage(Message msg)
	{ 
		int pos;
		switch (msg.what)
		{ 
		case FADE_OUT:
			hide(); 
			break; 
		case SHOW_PROGRESS:
			pos = setProgress();
			if (!isDragging && isShowing && playerControl.isPlaying())
			{ 
				msg = handler.obtainMessage(SHOW_PROGRESS);
				handler.sendMessageDelayed(msg, 1000 - (pos % 1000));
			} 
			break;