【发布时间】:2014-03-15 23:16:41
【问题描述】:
所以最近,我一直在尝试制作自己的马里奥游戏(为我自己,可能是给我的其他朋友看)。游戏包括按钮。当我单击其他游戏中的按钮时,它会播放声音。我很想将这个功能添加到我的游戏中。问题是,它不会播放。我的源代码是:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JButtonClick {
JButton test = new JButton("Click Me!");
JPanel panel = new JPanel();
public void playSound(String soundName)
{
try
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
catch(Exception ex)
{
System.out.println("Error with playing sound.");
ex.printStackTrace( );
}
}
public JButtonClick() {
JFrame frame = new JFrame("Button-Click test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.add(panel);
frame.setSize(800, 600);
frame.setResizable(true);
frame.setVisible(true);
panel.add(test);
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
playSound("JButton.wav");
}
});
}
public static void main(String[] args) {
new JButtonClick();
}
}
我的 .wav 文件与此类在同一个包中。但它并没有播放我想要的声音,而是这样说:
java.io.FileNotFoundException: C:\Users\diego\workspace\Super Mario Bros 1\JButton.wav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at JButtonClick.playSound(JButtonClick.java:21)
at JButtonClick$1.actionPerformed(JButtonClick.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我做错了什么?!
【问题讨论】:
-
可能是指定了错误的路径?
-
确实,@JoshM 说的是实话。请理解您正在尝试将 wav 文件作为文件访问,并且默认目录是
user.dir的目录,而这不是您要查找的位置。如果可能,您应该将 wav 文件作为资源而不是文件来访问。 -
该资源是embedded-resource。在这种情况下,资源必须由
URL而不是File访问。请参阅标签的info page,了解形成URL的方法。 -
要么使用完整的源路径,要么(至少)从程序的根目录开始。如果您的文件与声音一起在根文件夹中,请使用
root/sound.wav -
FileNotFoundException通常因为找不到文件而被抛出。
标签: java swing audio jbutton embedded-resource