android 如何在播放下一首歌曲时不将seekBar设置为0

ewm0tg9j  于 2023-02-02  发布在  Android
关注(0)|答案(2)|浏览(169)

我想让mediaplayer播放下一首歌,并让seekbar不断前进。但是,现在seekbar在播放下一首歌时重置回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);
    }
  }
};
eiee3dmh

eiee3dmh1#

我自己刚刚创建了一个解决方案,涉及创建一个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);
  }
};
ugmeyewa

ugmeyewa2#

当播放下一首歌曲时,时间组seekBar.setProgress(0);

相关问题