开发者

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.

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)
        {}
    }
}


Most reliable solution is to create a progress bar and wait for the sounds to get loaded, so this will keep the user entertained a little as he can see something moving


Try

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. If not the fastest, it will be the most reliable solution.


You could add a delay I.E Thread.sleep(100); this will delay the thread for 100 milliseconds

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜