如何将KeyListener与音频一起使用?

时间:2014-07-18 23:12:17

标签: java

好吧,我正在尝试制作一个模拟启动板的程序。但为此,我需要打开程序并按下,例如," Q"关键,它播放声音。我怎么能这样做?简而言之:" Handshake指定一个键,当它被按下时,点击一个声音"如果你能回答,甚至感激不尽!的xD 我点击键盘上的一个按钮,出现错误,以下代码出现音乐:

try {
AudioInputStream sound = AudioSystem.getAudioInputStream (getClass () getResource       ("1.wav").); 
DataLine.Info info = new DataLine.Info (Clip.class, sound.getFormat ()); 
Clip clip = (Clip) AudioSystem.getLine (info); 
clip.open (sound); 
clip.start (); 
if (playing == false) {
clip.stop (); 
Else {} 
clip.start (); 
} 
} Catch (Exception e) {
JOptionPane.showMessageDialog (null, "Fatal err"); 
} 

我创建了一个KeyListener,这样当我点击时,会播放声音,但会出现以下错误:

java.lang.NullPointerException 
com.sun.media.sound.StandardMidiFileReader.getSequence at (Unknown Source) 
javax.sound.midi.MidiSystem.getSequence at (Unknown Source) 
com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream at (Unknown Source) 
javax.sound.sampled.AudioSystem.getAudioInputStream at (Unknown Source) 
at Programa.LaunchPad.som1 (LaunchPad.java: 41) 
Programa.LaunchPad at $ BotãoAção.keyReleased (LaunchPad.java: 201) 
java.awt.Component.processKeyEvent at (Unknown Source) 
at java.awt.Component.processEvent (Unknown Source) 
at java.awt.Container.processEvent (Unknown Source) 
java.awt.Window.processEvent at (Unknown Source) 
at java.awt.Component.dispatchEventImpl (Unknown Source) 
at java.awt.Container.dispatchEventImpl (Unknown Source) 
at java.awt.Window.dispatchEventImpl (Unknown Source) 
at java.awt.Component.dispatchEvent (Unknown Source) 
java.awt.KeyboardFocusManager.redispatchEvent at (Unknown Source) 
java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent at (Unknown Source) 
java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent at (Unknown Source) 
java.awt.DefaultKeyboardFocusManager.typeAheadAssertions at (Unknown Source) 
java.awt.DefaultKeyboardFocusManager.dispatchEvent at (Unknown Source) 
at java.awt.Component.dispatchEventImpl (Unknown Source) 
at java.awt.Container.dispatchEventImpl (Unknown Source) 
at java.awt.Window.dispatchEventImpl (Unknown Source) 
at java.awt.Component.dispatchEvent (Unknown Source) 
java.awt.EventQueue.dispatchEventImpl at (Unknown Source) 
java.awt.EventQueue.access at $ 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) 
java.security.ProtectionDomain at $ 1.doIntersectionPrivilege (Unknown Source) 
java.security.ProtectionDomain at $ 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) 
java.security.ProtectionDomain at $ 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)

我正在使用图书馆AWT和Swing。 (使用JPanel,ImageIcon,声音)

1 个答案:

答案 0 :(得分:1)

更新的问题看起来就是这个部分:

getClass().getResource("1.wav")

我打赌没有做你认为的事情,并且返回null。

逐步调试调试器中的代码,看看那里发生了什么,看看是否/为什么找不到你的wav。