j2me播放多个声音文件的问题

发布于 2024-11-02 22:49:11 字数 857 浏览 1 评论 0原文

我在 Eclipse 中的 j2me MIDP2 中开发的游戏中播放超过 2 个声音文件时遇到问题。 请告诉我播放多个“wav”声音文件的最佳方法。 我创建了以下方法,该方法在程序启动时调用一次

public void setSound()
        {
            System.out.println("Sound on");
            try {
                p1=Manager.createPlayer(is1, "audio/X-wav");
                p2=Manager.createPlayer(is2, "audio/X-wav");
                p3=Manager.createPlayer(is3, "audio/X-wav");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MediaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

,每次我需要播放其中一种声音时,我都会停止其他两个播放器(以确保它们中没有一个正在运行 p2.stop(); p3 .stop();)并启动第三个(p1.start();),每次我有两个播放器停止(处于 PREFETCHED 状态)时,第三个播放器就不会运行并抛出异常。

I have a problem with playing more than 2 sound files in a game I'm developing now in j2me MIDP2 in eclipse.
Please advice me the best way for playing multiple "wav" sound files.
I created the following method that is called once when the program starts

public void setSound()
        {
            System.out.println("Sound on");
            try {
                p1=Manager.createPlayer(is1, "audio/X-wav");
                p2=Manager.createPlayer(is2, "audio/X-wav");
                p3=Manager.createPlayer(is3, "audio/X-wav");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MediaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

and every time I need to play one of the sounds I stop the two other players (to insure that no one of them is running p2.stop(); p3.stop();) and start the third one (p1.start();) and every time I have two players stopped (being in PREFETCHED State) the third one is not running and exceptions are thrown.

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

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

发布评论

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

评论(2

断舍离 2024-11-09 22:49:11

Java ME 规范对此没有具体说明,但在实际设备上,您通常一次只能分配一个玩家,因此仅停止一个玩家是不够的。另请参阅此论坛帖子。我发现即使在玩新玩家之前正确地重新分配玩家也是一项挑战,因此 在我自己的代码中,我诉诸于等待一种声音结束,然后再尝试播放新的声音。

The Java ME spec is not specific about this, but on actual devices you can usually have only one allocated player at a time, so just stopping a player is not enough. See also this forum post. I found it is a challenge even to deallocate a player properly before playing a new one, so in my own code I resorted to waiting for one sound to end before trying to play a new one.

红衣飘飘貌似仙 2024-11-09 22:49:11

在j2me中只有一个播放器。不支持超过1个播放器。如何执行一个接一个或任何其他声音的声音。如果你想播放多个声音意味着那么你遵循下面的编码

public class AudioPlayer implements javax.microedition.media.PlayerListener{

Player player;
int count=0;

public void playMedia(){

try{

player = Manager.createPlayer(getClass().getResourceAsStream(audioFiles[count]),"audio/x-amr");
player.addPlayerListener(this);
player.start();

}
catch (Exception e) {
return ;
}
}


public void playerUpdate(Player p,String event,Object eventData) 
{

        //Playing next audio file after completion of current audio.
}

}

in j2me there is only one player.More than 1 players are not supported.How execute the sound one sound after another or any other.If u want playing multiple sounds means then u follow the below coding

public class AudioPlayer implements javax.microedition.media.PlayerListener{

Player player;
int count=0;

public void playMedia(){

try{

player = Manager.createPlayer(getClass().getResourceAsStream(audioFiles[count]),"audio/x-amr");
player.addPlayerListener(this);
player.start();

}
catch (Exception e) {
return ;
}
}


public void playerUpdate(Player p,String event,Object eventData) 
{

        //Playing next audio file after completion of current audio.
}

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