another thing is the timer for seekBar its for the duration of the audio file the issue within it is that the audio file do (mini) stop every 1 sec so how to make seek bar update itself every second without doing any stops
seekBar2.setMax(mediaPlayer.getDuration());
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
seekBar2.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
public void reset(View v) {
mediaPlayer.reset();
}
Try this to Reset Your mediaPlayer
public void reset(View v) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
Try this to update your seekbar
private Handler mHandler = new Handler();
//you have to update Seekbar on UI thread
Your.Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar2.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
and whenyou need to update the
MediaPlayer
's position while user drag yourSeekBar
use below code
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress * 1000);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.