J2me播放器,播放MP3文件一个接一个



我不能一个接一个地播放MP3文件,当一个文件播放完时,另一个文件需要开始播放。我只能开始播放一个文件,当我把代码开始下一个文件时,它什么也不做。由于某些原因,我不能在文件期间使用thread.sleep()。还有别的办法吗?

//this is the basic code..
playq(String fname){
pl =  Manager.createPlayer(stream,"audio/mpeg");
pl.realize();
pl.prefetch();
pl.start();
// what should i use here?? pls i don't want to use thread.sleep..
playagain(fname);
}
void playagain(String fname){
        try {
    pl.stop();
    pl.deallocate();
   pl.close();
} catch (Exception ex) {}
 //change file name and stream and..
        playq(mp3f);
    }

你的代码应该而不是尝试从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的警告,等等)。

相关内容

最新更新