gpt4 book ai didi

java - mediaPlayer CompletionListener 在完成歌曲之前播放下一首歌曲

转载 作者:行者123 更新时间:2023-11-29 06:58:15 25 4
gpt4 key购买 nike

我已经编写了一个 mediaPlayer 应用程序,其中 mediaplayer.setOnCompletionListener 在歌曲完成之前被调用,每首歌曲都会继续...它会继续播放下一首歌曲,直到到达播放列表中的最后一首歌曲...请帮忙

这是我的代码:

 tracksListViewForSpeakers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Track track = playlistContents.get(position);
String filepath = track.getFilePath();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(filepath);
mediaPlayer.start();

// mediaPlayer.setVolume(0, 0);
} catch (IOException e) {
e.printStackTrace();
for(int i=0;i<4;i++)
System.out.println("MediaPlayer Exception");
}

}
});

完成监听器

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
onNextClicked(mp);
}
});

OnNextClickedMethod

public void onNextClicked(MediaPlayer mp){
int val = myModel.getCurrentPlayingIndex();
val=val+1;
Track track = currentPlayList.get(val);
String filepath = track.getFilePath();
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();
try {
mp.setDataSource(filepath);
mp.start();
// mediaPlayer.setVolume(0, 0);
} catch (IOException e) {
e.printStackTrace();
}

}

}

最佳答案

完成后使用它播放下一首歌曲

@Override
public void onCompletion(MediaPlayer arg0) {

if (isRepeat) {
// repeat is on play same song again
playSong(currentSongIndex);
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else {
// no repeat or shuffle ON - play next song
if (currentSongIndex < (songsList.size() - 1)) {
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
playSong(0);
currentSongIndex = 0;
}
}

}

那么播放歌曲的方法是这样的

public void  playSong(int songIndex){
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);

// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);

// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);

// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

或者你可以看到这个教程并实现这个

http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

关于java - mediaPlayer CompletionListener 在完成歌曲之前播放下一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com