I have a media intensive app (game), and am currently in the throws of a truly hellish bug.

I seem to have an extremely sporadic sound bug... every few dozen games, an IllegalStateException is sneaking out uncaught.

The problem is, the game works fine a great majority of the time, there is no rhyme or reason to the exception (save that it always occurs on a loadSound or playSound), and I think I've caught every possible place an IllegalStateException could be thrown.

I don't mind if audio drops here or there, I just need it to not crash.

The media is a combination of Wav's and midi's, several of them are time-critical (item pickups, etc). Because of this, I have a mediahandler than spawns a threads on media requests, and then runs a synchronized interface to this class, which physically loads and plays the media.

This works great on every other platform/handset I've tried it on.

Code Below:

public void loadSound(int s) {

try {

if ((loaded != s && !isPlaying) || forceLoad) {
forceLoad = false;

// if sound playing/running, stop and kill it
if (loaded != NONE) {
stopSound(false);
killSound(loaded);
}

//System.out.print("Attempting to Load: " + soundFiles[s] + ": ");
InputStream is = getClass().getResourceAsStream(soundFiles[s]);

try {
//#ifdef wav
if (soundFiles[s].endsWith("wav")) {
//System.out.print("WAV, ");
player = Manager.createPlayer(is, "audio/x-wav");

} else
//#endif
if (soundFiles[s].endsWith("mid")) {
//System.out.print("MID, ");
player = Manager.createPlayer(is, "audio/midi");
} else {
//System.out.print("UNKNOWN TYPE: ");
}

boolean success = false;


player.realize();
player.prefetch();
player.addPlayerListener(this);
} catch (IllegalStateException e) {
//ignore
} finally {
is.close();
}

loaded = s;

}
} catch (Exception real) {
//PacBowlMIDlet.output.append(soundFiles[s] + "(LOAD: "+real+")");
}
}

public void playSound(int s) {
try {

if (loaded != s) {
forceLoad = true;
loadSound(s);
} else {
if (isPlaying) stopSound(false);
//System.out.println("PreCached: SUCCESS");
}

if (player.getState() == Player.PREFETCHED) {
try {
player.start();
} catch (IllegalStateException e) {
// IGNORE
}
isPlaying = true;
}

} catch (Exception e) {
//System.out.println(IMP_NAME+"SndPlay: " + e);
}
}

public void killSound(int s) {

try {
if (player != null) {
stopSound(true);
try {
player.close();
} catch (IllegalStateException E) {
// IGNORE
}
player = null;
System.gc();
loaded = NONE;
}
} catch (Exception e) {
//System.out.println(IMP_NAME+"SndKill: " + e);
}

}

public boolean isPlaying() {

return isPlaying;

}


public void stopSound(boolean force) {

try {
if (player != null) {
if (player.getState() == Player.STARTED) {
isPlaying = false;
try {
player.stop();
} catch (IllegalStateException e) {
}
}
}
} catch (Exception e) {
//System.out.println(IMP_NAME+"SndStop: " + e);
}
System.gc();
}

public void playerUpdate(Player p, String event, Object data) {
synchronized (this) {
try {
if (event == PlayerListener.END_OF_MEDIA) {
stopSound(false);
}
} catch (Exception e) {
//System.out.println(IMP_NAME+"SndUpdt: " + e);
}
}
}

any and all suggestions would be greatly appreciated, thanks!

edit: clarification: my media handler class works great, obviously this particular media interface is nokia specific. Unfortunatley, the only handset I have available to test is the 3650. It works great on the emulator, however.;

-Scott