我将seekbar
与MediaPlayer
一起使用。对于seekbar
进度,我使用的是AsyncTask
,
我正在使用setMax并在Miliseconds中进步,以便进度顺利。
我在播放seekBar
时手动更改video
时遇到了一些问题。
搜索栏
max = mediaPlayer.getDuration();
mSeekBar.setMax(max);
这是 SeekBarTask
public class SeekBarTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
while (!isCancelled() && mediaPlayer != null && isPlaying) {
try {
Thread.sleep(10);
publishProgress();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
cancel(true);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
if (mediaPlayer != null) {
if (mSeekBar != null) {
mSeekBar.setProgress(mediaPlayer.getCurrentPosition());
}
if (currentTimeText != null) {
currentTimeText.setText(convertToTimeFormat(mediaPlayer.getCurrentPosition()));
}
}
}
SeekbarChnageListner
SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
Debug.d(TAG, "onProgressChanged : Progress = " + progress + " fromUser = " + fromUser);
if (mediaPlayer != null) {
mediaPlayer.seekTo(progress);
currentTimeText.setText(convertToTimeFormat(progress));
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
以下是相同
的日志
02-04 12:33:02.702 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition7990
02-04 12:33:02.716 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition8003
02-04 12:33:02.721 26960-26960/sampleApp D/VideoPlayerFragment: onProgressChanged : Progress = 16140 fromUser = true
02-04 12:33:02.732 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition16140
02-04 12:33:02.734 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition16140
02-04 12:33:02.751 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition4
02-04 12:33:02.754 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition7
02-04 12:33:02.771 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition24
02-04 12:33:02.774 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition27
02-04 12:33:02.782 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition35
02-04 12:33:02.802 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition55
02-04 12:33:02.804 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition57
02-04 12:33:02.816 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition69
02-04 12:33:02.835 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition85
02-04 12:33:02.838 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition91
02-04 12:33:02.848 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition101
02-04 12:33:02.865 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition118
02-04 12:33:02.868 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition121
02-04 12:33:02.885 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition138
02-04 12:33:02.887 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition140
02-04 12:33:02.899 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition152
02-04 12:33:02.916 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition169
02-04 12:33:02.932 26960-26960/sampleApp D/VideoPlayerFragment: onProgressUpdate-- seekBarPosition184
&#13;
有人可以告诉我哪里弄错了。当我触摸seekBar时,搜索栏会更改为一些非常小的值,我已将其附加到日志中。