J2me播放器,逐个播放MP3文件

发布于 2024-12-05 22:28:07 字数 590 浏览 0 评论 0原文

我无法依次播放 MP3 文件,当 1 个文件播放完毕后,另一个文件需要开始播放。我只能开始播放 1 个文件,当我输入代码来启动下一个文件时,它什么也不做。由于某些原因,我无法在文件持续时间内使用 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);
    }

I am unable to play an MP3 file one after the other, when 1 file finishes playing the other file needs to start. I am only able to start playing 1 file, when I put code to start the next file it just does nothing. For some reasons I cannot use thread.sleep() for the duration of file. Is there any other way to do this?

//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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-12-12 22:28:07

您的代码不应该尝试从 catch 块中播放代码 - 一方面,只有在以下情况下才会调用它:生成了异常(您通常也不应该一揽子捕获异常 - 使用更具体的内容)。

您确定您不能使用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 的任何警告等)。

Your code should NOT attempt to play the code from a catch block - for one thing, it's only going to be called if an exception was generated (You also generally shouldn't be blanket-catching Exception - use something more specific).

Are you sure you can't use Thread.sleep()? Granted you're not really going to want to anyways (for instance, if the user can pause the clip...).

Instead, look into using the PlayerListener interface, and listen for END_OF_MEDIA events.

A really basic (as in, this isn't tested, and needs more work besides) example:

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();
            }
        }
    }
}

Please note that I'm not doing this completely correctly - I'm not doing exception handling, for example. Also, without knowing more about your situation, I don't know what other things to worry about. This should just be a simple answer to get you started looking at where you should be going with this.

(Also, I've never worked with the media player of JME, so I don't know any caveats about the GC there, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文