SeekBar seekBar = findViewById(R.id.seekbar); TextView currentTime = findViewById(R.id.current_time);
seekBar.setMax(mediaPlayer.getDuration()); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) {
mediaPlayer.seekTo(progress);
currentTime.setText(formatTime(mediaPlayer.getCurrentPosition()));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
private String formatTime(int millis) { String output = "00:00"; long seconds = millis / 1000; long minutes = seconds / 60; seconds = seconds % 60; if (minutes < 10) { output = "0" + minutes + ":"; } else { output = minutes + ":"; } if (seconds < 10) { output += "0"; } output += seconds; return output; }
这样就能正确显示进度条和当前时间了。