我想制作一个程序,在一个结束变量之后自动播放下一首歌;
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();
}
}
但是没有任何效果,在第一首歌结束后,什么也没发生
最佳答案
将无效代码更改为此
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/