Java如何等待剪辑结束

时间:2015-09-30 01:05:33

标签: java audio wait

我正在尝试播放一些小的音效,我通过加载剪辑,播放它的方法来做这个,然后关闭剪辑以允许稍后加载新的剪辑导致任何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。     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)     在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)     at java.awt.Component.processMouseEvent(Component.java:6519)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3312)     在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)

剪辑结束后如何添加clip.close()

1 个答案:

答案 0 :(得分:1)

尝试添加finally块以关闭连接

<?xml version="1.0" ?> <TRANSACTION_RESPONSE> <TRANSACTION> <TRANSACTION_ID>25429</TRANSACTION_ID> <MERCHANT_ACC_NO>02700701354375000964</MERCHANT_ACC_NO> <TXN_STATUS>F</TXN_STATUS> <TXN_SIGNATURE>a16af68d4c3e2280e44bd7c2c23f2af6cb1f0e5a28c266ea741608e72b1a5e4224da5b975909cc43c53b6c0f7f1bbf0820269caa3e350dd1812484edc499b279</TXN_SIGNATURE> <TXN_SIGNATURE2>B1684258EA112C8B5BA51F73CDA9864D1BB98E04F5A78B67A3E539BEF96CCF4D16CFF6B9E04818B50E855E0783BB075309D112CA596BDC49F9738C4BF3AA1FB4</TXN_SIGNATURE2> <TRAN_DATE>29-09-2015 07:36:59</TRAN_DATE> <MERCHANT_TRANID>150929093703RUDZMX4</MERCHANT_TRANID> <RESPONSE_CODE>9967</RESPONSE_CODE> <RESPONSE_DESC>Bank rejected transaction!</RESPONSE_DESC> <CUSTOMER_ID>RUDZMX</CUSTOMER_ID> <AUTH_ID /> <AUTH_DATE /> <CAPTURE_DATE /> <SALES_DATE /> <VOID_REV_DATE /> <REFUND_DATE /> <REFUND_AMOUNT>0.00</REFUND_AMOUNT> </TRANSACTION> </TRANSACTION_RESPONSE>