[안드로이드] MediaPlayer SeekBar 연동
MediaPlayer의 진행항태를 seekbar를 통하여 나타내고 싶었다 그래서 나는 CountDownTimer를 이용하여 작성 해보았다.
seekPlayBar = (SeekBar) findViewById(R.id.mp_seekbar_playbar);
mp = Cmp.getMp();
date = new Date();
date.setMinutes(0);
int sec = mp.getDuration() / 1000;
date.setSeconds(sec);
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
String str = formatter.format(date.getTime());
fulltime.setText(str);
if (str.equals("00:00")) {
new AlertDialog.Builder(MusicPlayerActivity.this)
.setMessage(new TextGroup().NETWORKERRORMESSAGE)
.setPositiveButton("확인",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
onBackPressed();
}
}).setNegativeButton("안함", null).show();
}
runtime.setText("00:00");
timer = new CountDownTimer(mp.getDuration() * 1000, 500) {
public synchronized void onTick(long millisUntilFinished) {
if (CoolTimerFlag) {
sum = sum + 500;
time = (int) sum / 1000;
date.setSeconds((int) time);
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
String str = formatter.format(date.getTime());
runtime.setText(str);
seekPlayBar.setProgress(time * 1000);
}
}
public void onFinish() {
}
};
seekPlayBar.setMax(mp.getDuration());
seekPlayBar.setProgress(mp.getCurrentPosition());
seekPlayBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
}
public synchronized void onStartTrackingTouch(SeekBar seekBar) {
CoolTimerFlag = false;
mp.pause();
}
public synchronized void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
sum = mp.getCurrentPosition();
mp.start();
Mpbutton[2].setBackgroundResource(R.drawable.btn_b_alb_stop);
CoolTimerFlag = true;
}
});