我有一组声音文件名。我希望媒体播放器一个接一个地播放。但是,在循环播放时,它们都是同时播放的。解决方案是什么?
public static void PlaySentence(Context ctx, ArrayList<String> words)
{
final ArrayList<MediaPlayer> players = new ArrayList<MediaPlayer>();
for (String t : words)
{
int sound_id = ctx.getResources().getIdentifier(t, "raw", ctx.getPackageName());
if(sound_id != 0) {
MediaPlayer newMP = MediaPlayer.create(ctx, sound_id);
newMP.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
players.remove(mp);
if (players.size() > 0)
{
Log.d("player", "size " + players.size());
players.get(0).start();
}
mp.release();
}
});
players.add(newMP);
}
}
players.get(0).start();
}
谢谢