Why the applet can not play sound?
//Here is the code,Could anybody help me find out why it does not work? Thank you!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example10_3 extends Applet implements ActionListener
{ AudioClip clip;
Button buttonPlay,
buttonLoop,
buttonStop;
public void init()
{ clip=getAudioClip(getCodeBase(),"ding.Wav");
//have ding.wav file in the same directory with the applet
buttonPlay=new Button("Play");
buttonLoop=new Button("Replay");
buttonStop=new Button("Stop");
buttonPlay.addActionListener(this);
buttonStop.addActionListener(this);
buttonLoop.addActionListener(this);
add(buttonPlay);
add(buttonLoop);
add(buttonStop);
}
public void stop()
{ clip.stop();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==buttonPlay)
{ clip.play();
}
else if(e.getSource()==buttonLoop)
{ clip.loop();
}
if(e.getSource()==buttonStop)
{ clip.stop();
}
}
}
Re: Why the applet can not play sound?
The only reason I can see for it not working is it can't find ding.Wav. I have run it on my system and it works fine.
Make sure the file ding.Wav is in the same directory as the class file and if you are running on a case sensitive OS that the case is correct. You should also try running the wav file directly from the OS to make sure it is outputting an audible sound.
Re: Why the applet can not play sound?
Thank you,but this still can not run in my computer, do you think it is OS dependent?
Re: Why the applet can not play sound?
What OS are you running on?
Try adding the following lines to your code just before you create the audio clip
and then run your applet. You should see the print out in the applet console and it will tell you if the sound file is visible.
Code:
File f = new File(getCodeBase().getFile(), "ding.Wav");
System.out.println(f+", Exists = "+f.exists());