1.
//同步seekbar与进度条时间
private void updateSeekBar(){
runOnUiThread(new Runnable() {
@Override
public void run() {
if (player != null){
int mCurrentPosition = player.getCurrentPosition() / 1000;//获取player当前进度,毫秒表示
int total = player.getDuration() / 1000;//获取当前歌曲总时长
sb.setProgress(mCurrentPosition);//seekbar同步歌曲进度
sb.setMax(total);//seekbar设置总时长
tv_total.setText(calculateTime(total));
tv_current.setText(calculateTime(mCurrentPosition));
}
mHandler.postDelayed(this,1000);//延迟一秒运行线程
}
});
}
2
//计算歌曲时间 private String calculateTime(int time){ int minute; int second; if (time >= 60){ minute = time / 60; second = time % 60; return minute + ":" +second; }else if (time < 60){ second = time; return "0:" + second; } return null; }
3.参考//拉动seekbar,同步歌曲播放 sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (player != null && fromUser){ player.seekTo(progress * 1000); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }
Android Building Audio Player Tutorial