method for playing music in game
I am working on a java RPG where i have different room objects. I am trying to get the program to play a song for a specific room. The program is an application so the play method for applets will not work. I have the rooms being instantiated and created in a GameWorld class that puts items foes, etc into the room. I would like the music playing to change whenever you go to a different room. Any help would be appreciated
[431 byte] By [
pcleadera] at [2007-10-1 13:14:52]

Actually, you could use the applet thing (sort of) by getting an audio clip
Applet.newAudioClip("URL of clip here"); <--static method so you don't need to have an applet instance.
Here's part of the code I use in Javoids to load the sounds into a HashTable:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResource(location));
final Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class,audioInputStream.getFormat()));
clip.addLineListener(new LineListener()
{
public void update(LineEvent lineEvent)
{
if (lineEvent.getType() == LineEvent.Type.STOP)
{
clip.stop();
clip.setMicrosecondPosition(0);
}
}
});
clip.open(audioInputStream);
sounds.put(sound,clip);
Feel free to rip off any of the code in the jar file (hosted on sourceforge) since it's all GPL'd.
Then all you need is a list of rooms and the sound file you should play when they enter the room. Go through the list of rooms until you find the room the character is in and play that sound.
Hope this helps.
dude, i am making one too!!!!!! so u want the user to open the file or what, i just figured out how to put music myself.......email me at ashwindec11@gmail.com ,
also, how are you doing ur RPG? like, how do the people move, what layout r u using, etc, cuz mine is very very simple........
This can be simplified if you aren't going to use the same structure:
1. Add a name to the enum Sound say NEW_SOUND
2. Add a line to associate the enum with a sound抯 file name soundNames.put(NEW_SOUND,"MyNewSound.wav");
3. Make a new SoundMapprivate final static SoundMap soundMap = new SoundMap();
4. Start a the SoundMap thread to load the file soundMap.start();
5. Call Media.play(NEW_SOUND); //kind of obfuscated behind other function calls
you could replace Media.Play(NEW_SOUND) with this:
final Clip clip = soundMap.getSound(NEW_SOUND);
setVolume(clip,Media.getVolume());
clip.setFramePosition(0);
clip.start();
You could extract everything you need from this sequence including setting the volume:
try
{
final SoundMap soundMap = new SoundMap();
soundMap.start();
soundMap.join();
final Clip clip = soundMap.getSound(SHIPDIE);
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN))
{
float value = (float) (50.0 / 100.0);
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
}
clip.setFramePosition(0);
clip.start();
}
catch (InterruptedException interruptedException)
{
interruptedException.printStackTrace();
}
I tried the code out to make sure it works. Please note this was designed using threads so that I could load all of the sounds, images, text files at once without proceeding until they were all loaded (it prevented a lot of bugs). If you don't have the .join() call then you'll get an exception because the resource didn't finish loading. This method was also chosen so that the files wouldn't have to be loaded every time they were used. I admit it can be kind of difficult working backwards out of someone else's code, but it can often bring you to a new understanding on how to do things.
Good luck with your game.