我当前正在播放一首歌,当我的第一首歌结束后,第二首歌开始播放。当我的第二首歌曲结束时,第三首歌曲应该开始播放,但不是。如何使用MediaPlayer做到这一点。
这是我的onCompletion代码:
@Override
public void onCompletion(MediaPlayer mp) {
didFinishFirst = true;
if(didFinishFirst) {
Uri songUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.two);
mp = MediaPlayer.create(getApplicationContext(), songUri);
mp.start();
mState = STATE_INITIALIZED;
didFinishSecond = true;
} else if(didFinishSecond){
Uri songUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.three);
mp = MediaPlayer.create(getApplicationContext(), songUri);
mp.start();
mState = STATE_INITIALIZED;
didFinishThird = true;
}
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// No need
}
});
}
答案 0 :(得分:2)
您需要再次将完成侦听器链接到MediaPLayer.create
返回的新媒体播放器实例。只有这样oncompletion
才会被触发
Override
public void onCompletion(MediaPlayer mp) {
didFinishFirst = true;
if(didFinishFirst) {
Uri songUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.two);
mp = MediaPlayer.create(getApplicationContext(), songUri);
mp.start();
mState = STATE_INITIALIZED;
didFinishSecond = true;
mp.setOnCompletionListener(this);
// ^^^^^^^^^^^^^^^^^^^^^^
} else if(didFinishSecond){
Uri songUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.three);
mp = MediaPlayer.create(getApplicationContext(), songUri);
mp.start();
mState = STATE_INITIALIZED;
didFinishThird = true;
mp.setOnCompletionListener(this);
// ^^^^^^^^^^^^^^^^^^^^^^
}
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// No need
}
});
}
答案 1 :(得分:-1)
我在这里所做的是在与UI线程分开的线程内启动MediaPlayer(回想起来,我应该只在异步任务内运行startMusic()代码,但是当我是新手程序员时,我将其写回了) 然后,我为每首歌曲的结尾设置一个收听者,并且根据该收听者,第一首歌曲结束后,我便开始播放下一首歌曲,依此类推。
我希望这会有所帮助!。
public static void startMusic(final Context context){
handlerMusic = new Handler();
handlerMusic.post(new Runnable() {
@Override
public void run() {
trackList.add(R.raw.diaz_choir);
trackList.add(R.raw.diaz_strum_in_mouth);
trackList.add(R.raw.diaz_underwater);
trackList.add(R.raw.diaz_trap);
for (int s = 0; s < trackList.size(); s++){
newList.add(trackList.get(s));
}
mMediaPlayer =
MediaPlayer.create(context,newList.get(songNum));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
songNum++;
/*
mp = MediaPlayer.create(context, newList.size() - 1);
mp.reset();
//mp.prepare();
mp.start();
*/
mMediaPlayer =
MediaPlayer.create(
context, newList.get(songNum));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
songNum++;
mMediaPlayer =
MediaPlayer.create(
context, newList.get(songNum));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
songNum++;
mMediaPlayer =
MediaPlayer.create(
context, newList.get(songNum));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
}
});
}
});
}
});
}
});
}