I created classto play sound when I click the buttons.
Here is the code:
public void playSound()
{
try
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("beep-1.wav"));
Clip clip = AudioSystem.getClip( );
clip.open(audioInputStream);
clip.start( );
}
catch(Exception e)
{
System.out.println("Error with playing sound.");
}
}
When I want to implement it in a method ButtonListener, it seems that the sound is not playing.
Here is the code ButtonListener:
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (replayButton == e.getSource())
{
playSound();
}
}
}
What is wrong with the code?
EDIT:
Basically, I am trying to create a simple memory game, and I want to add sound to the buttons when pressed.
SOLVED:
It appears that the audio file downloaded from Soundjay is having a problem, and therefore the audio file cannot be played. @_ @
source
share