播放下一首歌曲时如何不将 seekBar 设置为 0
How to not have seekBar set to 0 when next song is played
我想让 mediaplayer
播放下一首歌曲,并让搜索栏不断前进。但是,现在播放下一首歌曲时搜索栏会重置为 0。
当播放下一首歌曲时,如何让 seekBar 不重置回 0?
这是我的 onCompletion
方法:
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
if(currentTrack < tracks.length) {
currentTrack++;
if (currentTrack >= 2) {
currentTrack = 0;
}
mp = MediaPlayer.create(this, tracks[currentTrack]);
mp.setOnCompletionListener(this);
if (currentTrack > 0) {
mp.seekTo(tracks[currentTrack]);
}
handler.post(UpdateSongTime);
mp.start();
}
}
这是更新所有定位的线程:
private Runnable UpdateSongTime = new Runnable() {
@Override
public void run() {
if(mp.isPlaying()) {
startTime = mp.getCurrentPosition() / 1000;
seekBar.setMax(songEndTime);
int currentSongEndTime = songEndTime - mp.getCurrentPosition()/1000;
startTimeText.setText(convertSeekLength(startTime));
String endTime = "-" + convertSeekLength(currentSongEndTime);
songDisplay.setText(songArrayList.get(currentTrack).getTitle());
endTimeText.setText(endTime);
seekBar.setProgress(startTime);
handler.postDelayed(this, 100);
}
}
};
播放下一首歌曲时设置的时间 seekBar.setProgress(0);
我自己创建了一个解决方案,涉及创建一个 Arraylist of mediaPlayers
而不是仅仅制作 one mediaPlayer
.
如果有人需要,这里是工作代码:
public void onCompletion(MediaPlayer mediaPlayer) {
for(int i = 0; i < currentTrack; i++) {
trueSeek += mediaPlayers.get(i).getCurrentPosition();
}
currentTrack++;
if(currentTrack >= mediaPlayers.size()){
currentTrack = 0;
}
handler.post(UpdateSongTime);
mediaPlayers.get(currentTrack).start();
}
和线程:
private Runnable UpdateSongTime = new Runnable() {
@Override
public void run() {
if(currentTrack == 0){
trueSeek = mediaPlayers.get(currentTrack).getCurrentPosition();
startTime = trueSeek;
}
if(currentTrack > 0){
int tempSeek = 0;
for(int i = 0; i < currentTrack; i++){
tempSeek += mediaPlayers.get(i).getCurrentPosition();
}
trueSeek = mediaPlayers.get(currentTrack).getCurrentPosition() + tempSeek;
startTime = trueSeek;
}
seekBar.setMax(songEndTime);
int currentSongEndTime = songEndTime - trueSeek;
startTimeText.setText(convertSeekLength(startTime));
String endTime = "-" + convertSeekLength(currentSongEndTime);
songDisplay.setText(songArrayList.get(currentTrack).getTitle());
endTimeText.setText(endTime);
seekBar.setProgress(startTime);
handler.postDelayed(this, 100);
}
};
我想让 mediaplayer
播放下一首歌曲,并让搜索栏不断前进。但是,现在播放下一首歌曲时搜索栏会重置为 0。
当播放下一首歌曲时,如何让 seekBar 不重置回 0?
这是我的 onCompletion
方法:
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
if(currentTrack < tracks.length) {
currentTrack++;
if (currentTrack >= 2) {
currentTrack = 0;
}
mp = MediaPlayer.create(this, tracks[currentTrack]);
mp.setOnCompletionListener(this);
if (currentTrack > 0) {
mp.seekTo(tracks[currentTrack]);
}
handler.post(UpdateSongTime);
mp.start();
}
}
这是更新所有定位的线程:
private Runnable UpdateSongTime = new Runnable() {
@Override
public void run() {
if(mp.isPlaying()) {
startTime = mp.getCurrentPosition() / 1000;
seekBar.setMax(songEndTime);
int currentSongEndTime = songEndTime - mp.getCurrentPosition()/1000;
startTimeText.setText(convertSeekLength(startTime));
String endTime = "-" + convertSeekLength(currentSongEndTime);
songDisplay.setText(songArrayList.get(currentTrack).getTitle());
endTimeText.setText(endTime);
seekBar.setProgress(startTime);
handler.postDelayed(this, 100);
}
}
};
播放下一首歌曲时设置的时间 seekBar.setProgress(0);
我自己创建了一个解决方案,涉及创建一个 Arraylist of mediaPlayers
而不是仅仅制作 one mediaPlayer
.
如果有人需要,这里是工作代码:
public void onCompletion(MediaPlayer mediaPlayer) {
for(int i = 0; i < currentTrack; i++) {
trueSeek += mediaPlayers.get(i).getCurrentPosition();
}
currentTrack++;
if(currentTrack >= mediaPlayers.size()){
currentTrack = 0;
}
handler.post(UpdateSongTime);
mediaPlayers.get(currentTrack).start();
}
和线程:
private Runnable UpdateSongTime = new Runnable() {
@Override
public void run() {
if(currentTrack == 0){
trueSeek = mediaPlayers.get(currentTrack).getCurrentPosition();
startTime = trueSeek;
}
if(currentTrack > 0){
int tempSeek = 0;
for(int i = 0; i < currentTrack; i++){
tempSeek += mediaPlayers.get(i).getCurrentPosition();
}
trueSeek = mediaPlayers.get(currentTrack).getCurrentPosition() + tempSeek;
startTime = trueSeek;
}
seekBar.setMax(songEndTime);
int currentSongEndTime = songEndTime - trueSeek;
startTimeText.setText(convertSeekLength(startTime));
String endTime = "-" + convertSeekLength(currentSongEndTime);
songDisplay.setText(songArrayList.get(currentTrack).getTitle());
endTimeText.setText(endTime);
seekBar.setProgress(startTime);
handler.postDelayed(this, 100);
}
};