所以我正在学习如何使用音频剪辑,到目前为止,我的程序应该在点击按钮后播放声音,但在此之前,我在AudioClip上得到了一个nullpointexception。
public class soundtest extends JFrame implements ActionListener {
URL url = this.getClass().getResource("/Sounds/gameover.wav");
AudioClip clip = Applet.newAudioClip(url);
JButton button = new JButton("Play");
public soundtest() {
super("Swing Window");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
button.addActionListener(this);
add(button);
setVisible(true);
}
public static void main(String[] args) {
new soundtest();
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
clip.play();
}
}
}我甚至尝试过其他人的代码,但仍然得到一个错误。
我的音频文件绝对是在正确的位置...
发布于 2015-06-08 10:23:47
Java抛出了一个NullPointerException,因为clip是一个null引用(它没有引用对象)。您提供的代码看起来有点不对劲,因为它缺少实例化对象的new关键字。
你试过这个吗?:
AudioClip clip = new AudioClip(url.toString());Here's the documentation,你也许应该去看看。
https://stackoverflow.com/questions/30700456
复制相似问题