How to handle multiple sounds in Java ME
Article Metadata
Code Example
Source file: Media:MultipleSoundsMIDletSource.zip
Installation file: Media:MultipleSoundsMIDletBinaries.zip
Tested with
Devices(s): C3-01, Asha 305, E7-00
Compatibility
Platform(s): Series 40, Symbian Belle
Device(s): All
Article
Created: jaakl
(15 Sep 2009)
Updated: skalogir
(04 Nov 2011)
Reviewed: skalogir
(20 Jun 2011)
Last edited: hamishwillee
(14 Jun 2013)
Most phones will throw an exception if you try to keep two sounds loaded at the same time, so the trick is to deallocate the previous sound before you play the next one. The code below shows a SoundManager class, which keeps track of all the sounds in your midlet in a hashtable. It also keeps track of the time that the last sound was played, because if you try to play two sounds too close to each other it will fail in most phones.
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
public class SoundManager
{
private static Hashtable mapSound = new Hashtable();
private static long timeLastSound = 0;
private static boolean enabled = true;
static public Player getPlayer(String sound)
{
Player player;
player = (Player)mapSound.get(sound);
if (player == null)
{
try
{
String fileSpec = "/";
fileSpec += sound;
InputStream is = SoundManager.class.getResourceAsStream(fileSpec);
if (fileSpec.endsWith("amr"))
player = Manager.createPlayer(is, "audio/amr");
else if (fileSpec.endsWith("mid"))
player = Manager.createPlayer(is, "audio/midi");
mapSound.put(sound, player);
}
catch (Exception ex)
{
DebugStuff.print(ex.toString());
}
}
return player;
}
public static void playSound(String sound)
{
if (!enabled) return;
if (Math.abs(System.currentTimeMillis() - timeLastSound) < 250)
{
DebugStuff.print("tried to play 2 sounds too soon");
return;
}
timeLastSound = System.currentTimeMillis();
Player player = getPlayer(sound);
try
{
Enumeration enumValues;
Player playerOther;
for (enumValues = mapSound.elements(); enumValues.hasMoreElements() ;)
{
playerOther = (Player)enumValues.nextElement();
if (playerOther != player && playerOther.getState() == Player.STARTED)
playerOther.deallocate();
}
if (player.getState() == Player.STARTED)
{
DebugStuff.print("sound already started");
return;
}
player.start();
}
catch (Exception e)
{
DebugStuff.print(e.toString());
}
}
public static void setEnabled(boolean enabled)
{
SoundManager.enabled = enabled;
}
}
How to call use the SoundManager:
SoundManager.playSound("sound1.amr");
(Notice I am using the amr format instead of wav. This is because amr is much smaller, has more or less the same quality and is equally well supported as the wav format.)


CrisSS - First Play Sond
I have problems in the first time I play the sond (.mid). Some times run Ok , I close the aplication and I open the aplication one more time and I catch a Null Pointer Exception. I see in one site the following code exemple to play a sond:
....
try { InputStream is = getClass().getResourceAsStream("/music/music.mid"); Player p = Manager.createPlayer(is, "audio/mid"); p.realize(); p.prefetch(); p.prefetch(); p.prefetch(); p.prefetch(); p.start(); } catch (IOException ioe) { } catch (MediaException me) { }....
Calling prefetch() multiple times is for "wait" allocating memory?
This is a correct pratice?
Edited:--------------------------------
Sorry for the coment out of topic: The error in the code is a wrong audio type: "audio/mid" the right is "audio/midi"
Hamishwillee - @CrisSS - Discussion boards
Hi CrisSS
Unless you're asking questions directly related to the above article, I suggest you try raising this as a discussion board post.
Regards
Hamishhamishwillee 04:22, 5 July 2012 (EEST)