简体   繁体   中英

Issues with playing sounds on the BlackBerry

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.

There is no good solution to the problem, but here is how to mitigate it:

  • Create a separate thread to play the sounds (that is the thread that will call .start() on the Players, since that is the function that hangs)
  • If the frame rate of your game is too high and in consequence you are using 100% of the CPU, the media player will not get enough time to prepare the buffer between frames, so it will take a long time to run. Lower the frame rate a little to help.
  • At the Developer Conference, RIM showed code for a Wave Mixer that should fix the problem by keeping a player open while mixing incoming sound files into the stream. However they have not yet released the code.
  • On OS 7, under high cpu load, the midi files mess up with the frame rate, so avoid it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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