我创建了一个class
,以便在我点击按钮时播放声音。
以下是代码:
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.");
}
}
当我想将它实现到ButtonListener
方法中时,似乎没有声音播放。
这是ButtonListener
代码:
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (replayButton == e.getSource())
{
playSound();
}
}
}
这段代码有什么问题?
编辑:
我正在尝试创建一个简单的记忆游戏,并且想在点击按钮时添加声音。
已解决:
看起来我从Soundjay下载的音频文件存在问题,因此无法播放。 @_@