Java 如何等待剪辑结束

Java How to wait for a clip to end

我正在尝试播放一些小的音效,我正在通过一种方法来执行此操作,该方法加载剪辑、播放它,然后应该关闭剪辑以允许稍后加载新的剪辑而无需导致任何 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.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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:720)
at java.awt.EventQueue.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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 bytes/frame 的行,不支持小端。 在 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.access300 美元(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()

尝试添加 finally 块以关闭连接

finally { if (clip!= null) { try { clip.close() } catch (Exceptione e) { /* ignored */} }