你好我想平稳地更新搜索栏,每秒更新一次,所以我创建了一个方法,使用计时器每秒更新搜索栏,但它再次更新歌曲再次在每一秒我搜索到的所有地方但没有得到解决方案我不想使用任何我想使用此方法修复的处理程序
private void updateseekbar() {
seekBar.setMax(mediaPlayer.getDuration());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},1000,1000);
这里我已经实现了歌曲开始时的方法
public void play(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, songlist.get(index));
mediaPlayer.start();
updateseekbar();
imageview.setImageResource(R.drawable.img_btn_play_pressed);
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
imageview.setImageResource(R.drawable.img_btn_pause);
} else if (mediaPlayer != null) {
mediaPlayer.start();
imageview.setImageResource(R.drawable.img_btn_play_pressed);
}
这是我的完整代码
private SeekBar seekBar;
TextView startduration,totalduration;
ImageView imageview, forwardd, backword, next, previous;
ArrayList<Integer> songlist = new ArrayList<>();
MediaPlayer mediaPlayer;
int seektime = 5000;
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
imageview = (ImageView) findViewById(R.id.imageview);
forwardd = (ImageView) findViewById(R.id.forwardd);
backword = (ImageView) findViewById(R.id.backword);
next = (ImageView) findViewById(R.id.next);
previous = (ImageView) findViewById(R.id.previous);
startduration= (TextView) findViewById(R.id.startduration);
totalduration= (TextView) findViewById(R.id.totalduration);
songlist.add(R.raw.diljaniya);
songlist.add(R.raw.stand);
songlist.add(R.raw.haledil);
songlist.add(R.raw.myname);
songlist.add(R.raw.fiftyways);
songlist.add(R.raw.desiboys);
previous = (ImageView) findViewById(R.id.previous);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.seekTo(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
forwardd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null) {
int med = mediaPlayer.getCurrentPosition();
if (med <= mediaPlayer.getDuration()) {
mediaPlayer.seekTo(med + seektime);
seekBar.setProgress(med+seektime);
}
} else {
}
}
});
backword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null) {
int med = mediaPlayer.getCurrentPosition();
if (med - seektime >= 0) {
mediaPlayer.seekTo(med - seektime);
seekBar.setProgress(med-seektime);
} else {
}
} else {
}
}
});
}
public void play(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, songlist.get(index));
mediaPlayer.start();
updateseekbar();
imageview.setImageResource(R.drawable.img_btn_play_pressed);
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
imageview.setImageResource(R.drawable.img_btn_pause);
} else if (mediaPlayer != null) {
mediaPlayer.start();
imageview.setImageResource(R.drawable.img_btn_play_pressed);
}
}
public void previouss(View v) {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
}
if (index < songlist.size() - 1) {
mediaPlayer = MediaPlayer.create(this, songlist.get(index));
mediaPlayer.start();
seekBar.setProgress(0);
} else {
index = 0;
return;
}
index++;
}
public void next(View v) {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
}
if (index < songlist.size()) {
mediaPlayer = MediaPlayer.create(this, songlist.get(index));
mediaPlayer.start();
seekBar.setProgress(0);
} else {
index = 0;
return;
}
index++;
}
private void updateseekbar() {
seekBar.setMax(mediaPlayer.getDuration());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},1000,1000);
}
}
答案 0 :(得分:1)
将Timer声明为像这样的全局成员变量;
Timer timer=new Timer();
像这样更新updateseekbar方法中的歌曲
private void updateseekbar(Boolean songPlay) {
if(songPlay){
timer.schedule(new TimerTask() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},1000,1000);
handler.postDelayed(runnable,0);
}else {
timer.cancel();
timer.purge();
}
}
在播放像这样的歌曲时更新seekBar
updateseekbar(true);
像这样停止seekBar更新
updateseekbar(false);