gpt4 book ai didi

java - 媒体播放器在完成第一首歌曲后播放下一首歌曲

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:59 25 4
gpt4 key购买 nike

我想制作一个程序,在结束变量后自动播放下一首歌曲;

String path = "/mnt/sdcard/Music/";
String[] fileArray;
public int songNumber = 0;

MediaPlayer mPlayer = new MediaPlayer();
public boolean isPlaying = false;
public boolean isPaused = false;`

虚空是;

 private void playMusic(String song){
if(!isPlaying) {
try {
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mPlayer) {
songNumber = songNumber + 1;
playMusic("/mnt/sdcard/Music/" + fileArray[songNumber]);
}

});

mPlayer.reset();
Toast.makeText(getApplicationContext(), song, Toast.LENGTH_SHORT).show();
mPlayer.setDataSource(song);
mPlayer.prepare();
mPlayer.setOnCompletionListener((OnCompletionListener) this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}

mPlayer.start();
isPlaying = true;
}else{
stopMusic();
}
}

但是没有任何效果,第一首歌结束后什么也没有发生

最佳答案

将 void 上的代码更改为此

private void playMusic(String song){
if(!isPlaying) {
try {
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mPlayer) {
isPlaying = false;
mPlayer.reset();
songNumber = songNumber + 1;
playMusic("/mnt/sdcard/Music/" + fileArray[songNumber]);
}

});
Toast.makeText(getApplicationContext(), song, Toast.LENGTH_SHORT).show();
mPlayer.setDataSource(song);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}

mPlayer.start();
isPlaying = true;
}else{
stopMusic();
}
}

解决问题

关于java - 媒体播放器在完成第一首歌曲后播放下一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48624239/

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