05-17-2006, 01:38 PM | #1 |
FFR Player
|
Adding music to your java program
It's a simple question, but I can't find how to do it on Google. I'm probably searching for the wrong things, but I'm trying to figure out how to make a program play music from a mpg or wmp file(like the background music, and a fanfare when you beat a level). It should only take a few lines, I'm assuming.
Anyone know how? BTW, no, I don't want to know how to make my own music using swing or whatever. |
05-17-2006, 10:02 PM | #2 |
FFR Player
|
Re: Adding music to your java program
There's a way to do it using the Java Sound API. A much simpler way exists, which Kajakalaka Studios has used in the past and would be happy to share with you. Go to itmorr.com and in the downloads page you'll find a poorly made game named 'Safari Battleship'. That's not the source code, but it is a demo of what the background music is like when it's done.
You have to have a small sound clip. We're not sure if it buffers the sound and the limit is memory or what. Just trim down the sound clip and make it loopable if you can. Here's how to do background music on a Java app. //import your stuff. import sun.audio.AudioPlayer; import sun.audio.AudioStream; //make a new AudioPlayer. myBackgroundPlayer = AudioPlayer.player; //use a try block in case the file doesnt exist. try { myBackgroundMusic = new AudioStream(new FileInputStream("safari2.wav")); myData = myBackgroundMusic.getData(); myLoop = new ContinuousAudioDataStream(myData); } catch(IOException error) {} // play background music. myBackgroundPlayer.start(myLoop); if the song doesnt load, try a smaller song file and make sure it's the code's fault and not the song's fault for being too big. the way to do background music listed above is not very up to date and is probably rarely used, but it might be worth a try. also, make sure everything you just initialized in that code is declared either before being initialized or as an instance variable. if you want a copy of the source code we used, send an email to kajakalaka.studios@gmail.com. Last edited by SqNtz_xoombotvx; 05-17-2006 at 10:04 PM.. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|