I need help playing music in my java code. Right now I'm using the mouseClicked
method in my JApplet
, and each time I click on a certain location music, from a wav file is supposed to play. But when I run the applet, only the first object I click on will play, after nothing else will play and I cannot exit the applet (I have to force quit)
I have already tried using the clip.stop()
method after it has played, and after using that no sound plays at all.
this is my code in my class for playing music that's attached to my JApplet.
public void playMusic()
{
try{
File file = new File("island_music_x.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
this is code for an object that is supposed to be clicked in my applet to play music.
xpos5 = me.getX();
ypos5 = me.getY();
if
(xpos5 > rect5xco && xpos5 < rect5xco+rect5width && ypos5 >
rect5yco && ypos5 < rect5yco+rect5height)
rect5Clicked = true;
else
rect5Clicked = false;
when it is set to true I have code to play the music
if
(rect5Clicked == true)
{
try{
m5.playMusic5();}
catch (Exception e)
{}
}
When I click on an object, music is supposed to play, which occurs, however; I should be able to click on other objects after the first one and play music as well. But, the music only plays the first time. I am also unable to exit the appletviewer. Any help would be great thank you!!