Playing sounds in Java is a lot more complicated than one would expect, given the goal of it being “simple, object oriented and familiar”. I really was expecting to just have a class named “AudioPlayer” so that I could simply write:
failAmp = new AudioPlayer("MostAmazingSoundEver.wav");
//happy coder is happy
Unfortunately, the evil gods of Java decided to make it just a tad bit more complicated.
Further more the whole “write once run everywhere” came crashing down as well, because right now the project I am working on is clearly running ok on a Windows system, only to crash every time on an Ubuntu box. The causes (and implied fixes) still elude my team.
Finally, I’ve also had the weird and extremely unexpected surprise of code that suddenly just stopped working. I know for a fact that “we” didn’t break it, because we use version control and so we were able to go back to a previous revision of the code (that I know was stable and ran ok) and check to see what was going on. It was my guess at that point that a recent Java update might have broken something and I still believe that’s possible, though trying to build and run with an older target JRE didn’t fix the problem either.
Anyway, long story short, for the second time me and my team were hurdled on the path of figuring out how to play simple WAVE sound files in Java. I just want to stress that it’s the second time because, as I said above, the solution I came up with a year ago apparently had an expiration date of sorts. Google does offer a lot of answers, but some solutions that we’ve found simply don’t work or are not reliable enough. What looks to be the most elegant solution (similar to my first “ideal” example) uses “sun packages” and that is apparently a big no-no.
So, after some research, trial and error, we finally found something that looks like it works. At least for now… Here’s the code. They also have a few other nice examples on that website. Huge thanks from us to these guys, your website definitely helped us!
I’ll soon post the player we made for our project using the example above and hopefully this will be helpful to other coders out there. As usual, if you have any questions just use the comments, I’ll be glad to answer.