如何摆脱错误?

时间:2011-04-26 11:52:54

标签: java audio audio-streaming keylistener

我正在编写文本编辑器,它应该实际上是按下当前按下的键。我设法做到了,我甚至准备了文件。一切正常,但当符号数量超过29时,编译器会说:

javax.sound.sampled.LineUnavailableException: unable to obtain a line
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.connect(PulseAudioDataLine.java:279)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:102)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:289)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
at TextEditor$AreaListener.keyPressed(Main.java:81)
at java.awt.Component.processKeyEvent(Component.java:6161)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2801)
at java.awt.Component.processEvent(Component.java:5980)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4564)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4390)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1881)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:749)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1025)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:892)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:713)
at java.awt.Component.dispatchEventImpl(Component.java:4434)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4390)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

这是我的源代码的一部分,其中描述了一个实现KeyListener接口的类。这个类是我主要文本区域的监听器。

class AreaListener implements KeyListener {
    @Override public void keyPressed( KeyEvent e) {
        try {
            AudioInputStream result1 = AudioSystem.getAudioInputStream(new File("/home/nikkka/Desktop/alphabet/blank.wav"));;
            if(new Character(e.getKeyChar()).isLetter(e.getKeyChar()))
                result1 = AudioSystem.getAudioInputStream(new File("/home/nikkka/Desktop/alphabet/"+e.getKeyChar()+"_EDITOR.wav"));
            DataLine.Info info = new DataLine.Info(Clip.class, result1.getFormat());
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(result1);
            clip.start();
        } catch(Exception e1) {e1.printStackTrace();}}
    @Override public void keyReleased(KeyEvent e) {}
    @Override public void keyTyped(   KeyEvent e) {
    }
}

我看不出有什么问题?...而且,为什么适用于前29个符号的东西不应该更多?也许是因为我必须在clip.close()clip.start()关闭剪辑?请帮忙:-S

2 个答案:

答案 0 :(得分:2)

您收到此错误的原因是Line由于资源限制而无法使用,根据here所述的文档。

我认为您应该在使用Line方法后关闭Line.close()

修改的: 正确的做法可能如下:

  1. 在启动应用程序或创建用于处理输入区域的对象时创建Clip对象
  2. 在输入区域按下键时使用Clip对象
  3. 当应用程序退出或销毁输入区域时,
  4. stop()close() Clip
  5. 这样可以节省资源。

答案 1 :(得分:0)

这不是编译器错误消息。

这是一个运行时错误,我认为这意味着它无法读取声音文件中的数据。文件已损坏或格式不受支持。