简体   繁体   English

在Java ME中开始游戏之前加载声音

[英]Loading sound before game starts in Java ME

I used following logic to load the sound from a different thread to load it while my game goes on. 我使用以下逻辑从另一个线程加载声音,以在游戏进行时加载声音。 Though it works for very small wav files, sometimes, I have to wait till it loads. 尽管它适用于很小的wav文件,但有时还是要等到加载为止。

How can I make sure that it is loaded beforehand? 如何确保已预先加载?

   public class MusicPlayer implements Runnable {

    String sound;
    Player p;

    public MusicPlayer(String sound)
    {
        this.sound = sound;

        InputStream is = getClass().getResourceAsStream("/"+sound);
        try
        {
            p = Manager.createPlayer(is, "audio/X-wav");
        }
        catch(Exception e)
        {}

    }

    public void start()
    {
        Thread t = new Thread(this);
        t.start();
    }

    public void run()
    {
        try
        {
            p.start();
        }
        catch(Exception e)
        {}
    }
}

最可靠的解决方案是创建一个进度条,然后等待声音加载完毕,这样可以使用户保持愉悦感,因为他看到了一些动静

尝试

t.setPriority(Thread.MAX_PRIORITY);

Just in case anybody needs it, The best way I found is actually initialize the object in Main thread itself. 万一有人需要它,我发现的最佳方法实际上是在Main线程本身中初始化对象。 If not the fastest, it will be the most reliable solution. 如果不是最快的方法,它将是最可靠的解决方案。

You could add a delay IE Thread.sleep(100); 您可以添加一个延迟IE Thread.sleep(100); this will delay the thread for 100 milliseconds 这将使线程延迟100毫秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM