【发布时间】:2015-09-30 01:05:32
【问题描述】:
我正在尝试播放一些小的声音效果,我正在通过一种方法来加载剪辑,播放它,然后关闭剪辑以允许稍后加载新的剪辑,而不会导致任何LineUnavailable 问题。这是我使用的方法:
public void playClip(String filename){
Clip clip = null;
try{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource(filename));
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip)AudioSystem.getLine(info);
clip.open(inputStream);
clip.start();
}catch(Exception e){
e.printStackTrace();
}
}
现在,在clip.start() 之后,剪辑只保留在某处,用完一行,直到我收到以下错误:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1304)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1085)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1175)
at com.andrewlalisofficial.ChatClientCore.playClip(ChatClientCore.java:233)
at com.andrewlalisofficial.ChatClientWindow$4.mouseClicked(ChatClientWindow.java:207)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
javax.sound.sampled.LineUnavailableException:行格式为 PCM_SIGNED 44100.0 Hz,16 位,立体声,4 字节/帧,不支持 little-endian。 在 com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1304) 在 com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1085) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1175) 在 com.andrewlalisofficial.ChatClientCore.playClip(ChatClientCore.java:233) 在 com.andrewlalisofficial.ChatClientWindow$4.mouseClicked(ChatClientWindow.java:207) 在 java.awt.Component.processMouseEvent(Component.java:6519) 在 javax.swing.JComponent.processMouseEvent(JComponent.java:3312) 在 java.awt.Component.processEvent(Component.java:6281) 在 java.awt.Container.processEvent(Container.java:2229) 在 java.awt.Component.dispatchEventImpl(Component.java:4872) 在 java.awt.Container.dispatchEventImpl(Container.java:2287) 在 java.awt.Component.dispatchEvent(Component.java:4698) 在 java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501) 在 java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 在 java.awt.Container.dispatchEventImpl(Container.java:2273) 在 java.awt.Window.dispatchEventImpl(Window.java:2719) 在 java.awt.Component.dispatchEvent(Component.java:4698) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747) 在 java.awt.EventQueue.access$300(EventQueue.java:103) 在 java.awt.EventQueue$3.run(EventQueue.java:706) 在 java.awt.EventQueue$3.run(EventQueue.java:704) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 在 java.awt.EventQueue$4.run(EventQueue.java:720) 在 java.awt.EventQueue$4.run(EventQueue.java:718) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:717) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
剪辑完成后如何添加clip.close()?
【问题讨论】:
-
您可以使用
Clip#drain,但我过去在使用它时遇到过问题,因此改用LineListener代替example