你好,我希望順利地更新每一秒更新seekbar,所以我創建了一個使用定時器的方法,每秒更新seekbar,但它再次更新歌曲nd再次每秒我有搜查無處不在,但沒有得到解決方案,我不希望使用任何處理程序,我想用這種方法Seekbar一遍又一遍地更新整首歌
private void updateseekbar() {
seekBar.setMax(mediaPlayer.getDuration());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},1000,1000);
這裏來解決我有implemente當歌啓動方法
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);
}
}
可能[SeekBar和m edia播放器在Android](https://stackoverflow.com/questions/17168215/seekbar-and-media-player-in-android) –
我已經更新了問題的完整代碼 –
沒有它的不重複 –