我正在尝试用mediaPlayer.setOnCompletionListener()
播放下一首歌,但它只播放下一首歌,之后歌曲结束了媒体播放器停止。如何让媒体播放器一直播放?
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList)b.getParcelableArrayList("songList");
position = b.getInt("pos",0);
u = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.stop();
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
u = Uri.parse(mySongs.get(position + 1).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u);
mediaPlayer.start();
}
});
答案 0 :(得分:0)
您无需等到onCompletion。只需一次创建两个玩家,然后使用setNextMediaPlayer排队第二个玩家
u1 = Uri.parse(mySongs.get(position).toString());
u2 = Uri.parse(mySongs.get(position + 1).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u1);
mediaPlayerNext = MediaPlayer.create(getApplicationContext(), u2);
mediaPlayer.setNextMediaPlayer(mediaPlayerNext);
并且您在第二首歌之后没有收到onCompletion
,因为您将mediaPlayer
值替换为对没有收听者设置的新播放器的引用。将mediaPlayer.setOnCompletionListener()
添加到onCompletion handler
。你也不需要准备旧玩家