您的代码不应尝试从catch
块中播放代码 - 一方面,只有在生成异常时才会调用它(您通常也不应该笼统地捕捉Exception
- 使用更具体的东西)。
你确定不能用Thread.sleep()
?当然,无论如何您都不会真的想要(例如,如果用户可以暂停剪辑......)。
相反,研究使用PlayerListener
接口,并监听END_OF_MEDIA
事件。
一个非常基本的(例如,这没有经过测试,并且需要更多的工作)示例:
public class PlayerRunner implements PlayerListener {
private final String[] songFiles;
private int songIndex = 0;
public PlayerRunner(String[] songs) {
this.songFiles = songs;
}
public void start() {
playerUpdate(null, null, null);
}
// This method is required by the PlayerListener interface
public void playerUpdate(Player player, String event, Object eventData) {
// The first time through all parameters will be blank/null...
boolean nextSong = (event == null);
if (event == PlayerListener.END_OF_MEDIA) {
player.stop();
player.dallocate();
player.close();
nextSong = index < songIndex.length;
}
if (nextSong) {
String fileName = songFiles[index++];
if (fileName != null) {
Player pl = Manager.createPlayer(fileName, "audio/mpeg");
pl.addPlayerListener(this);
pl.realize();
pl.prefetch();
pl.start();
}
}
}
}
请注意,我没有完全正确地执行此操作 - 例如,我没有进行异常处理。另外,在不了解您的情况的情况下,我不知道还有什么需要担心的。这应该只是一个简单的答案,让您开始了解您应该去哪里。
(另外,我从未使用过 JME 的媒体播放器,所以我不知道关于 GC 的任何警告等)。