LOL! 'Linux sound' was enough to make Jamie
Zawinski dump Linux and convert to Apple and
MacOS.
I didn't get this. Did you say this ironically? I have no idea who is
he (other than the small bio wikipedia provides). But anyway, when I
said "linux sound system" I was talking about the chaotic API and
Services for sound in linux (ALSA, OSS, Pulse etc.). They can't seem
to go along together. Once a program uses one of the APIs, the next
can't use another. So a lot of programs cannot run together. In my
case, I was running RealPlayer, VirtualBox and my Java app. VirtualBox
probably was "stealing the sound", so when I stopped it, it worked!
No worries.
I can only imagine that any implementation based
on a Thread would be 'cleaner' than what I did
in that example above! Note that (as was pointed
out to me in the last 72ish hours) a Runnable
can do most anything that a Thread can.
Yes, in case anybody needs it, my (rather dangerous) code is this:
public class Utility {
private static Map<String, File> sndCache = new
ConcurrentHashMap<String, File>();
public static synchronized File getSound(String url) {
if ( sndCache.containsKey(url) )
return sndCache.get(url);
URL sndURL = Main.class.getResource("/res/snd/" + url);
if ( sndURL != null ) {
try {
sndCache.put(url, new File(sndURL.toURI()));
return sndCache.get(url);
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
} else {
System.err.println("Couldn't find file: " + url);
return null;
}
}
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(getSound(url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
}
I just create a thread with a runnable and run it on the fly. Although
I didn't need it, this way the sound cannot be stopped. So change
appropriately.
Thanks again..