views:

59

answers:

1

I am having the following problem when playing sounds on the blackberry:

  • The first time a sound is played, the app hangs for ~500 ms (not when the player is created, just when the sound is actually played)
  • There is a delay between the call and the sound being played

I have tested on a physical device in addition to the simulator. On the simulator, there must a bug, because it takes seconds before the sounds start (I have heard this is a problem with the simulator, so...).

I also have tested .wav vs .mp3 files, and it gives pretty much the same result.

Here is the code I use to load a player:

            stream = new Object().getClass().getResourceAsStream(fileName);
            result = javax.microedition.media.Manager.createPlayer(stream, fileName.endsWith(".wav")?"audio/x-wav":"audio/mpeg");
            if (prefetch) {
                result.prefetch();
            }

I then store the player reference, that I use later to play the sound with the following code:

            javax.microedition.media.Player player = (javax.microedition.media.Player)resources[soundId];
            if (player != null) {
                    //#debug
                    System.out.println("Player state: " + player.getState());
                    if (player.getState() != javax.microedition.media.Player.STARTED) {
                        player.setLoopCount(loopCount);
                        player.start();   
                    }
            }

I am positive the delay is due to the sound playing because I have a switch to turn it off and the delays goes away when I do that. Is there something I am doing wrong?

Update

Nothing worked, so I ended up changing the sounds to midi files which are handled better. If any one finds a solution, I would still like to hear it however.