简体   繁体   English

如何摆脱错误?

[英]How to get rid of error?

I'm writing text editor that is supposed to actually SAY the current key that is pressed. 我正在编写文本编辑器,该文本编辑器实际上应该说出所按下的当前键。 I managed to do that, I even prepared files. 我设法做到了,甚至准备了文件。 And everything works just fine, but when the amount of symbols becomes more than 29, the compiler says: 一切都很好,但是当符号数量超过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)

Here is the part of my source code where is described a class that implements KeyListener interface. 这是我的源代码部分,其中描述了实现KeyListener接口的类。 This class is a listener of my main text area. 此类是我主要文本区域的侦听器。

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) {
    }
}

I can't see what's the problem?... Moreover, why something that works for first 29 symbols, shouldn't work on more? 我看不出有什么问题?...此外,为什么有些对前29个符号有用的东西不应该在更多的东西上起作用? Maybe it's because I have to close the clip by clip.close() after clip.start() ? 也许是因为我必须关闭剪辑clip.close()clip.start() Please, help :-S 请帮助:-S

You get this error because the Line is not available due to resource restrictions, according to the documentations described here . 根据此处描述的文档,由于Line受限, Line不可用,您会收到此错误。

I think you should close the Line after you used it with the Line.close() method. 我认为在将Line.close()方法与Line后,应该关闭Line

EDIT : The proper way of doing it could be the following: 编辑 :正确的方法可能是以下:

  1. create a Clip object when starting the application or creating objects for handling the input area 启动应用程序时创建一个Clip对象或创建用于处理输入区域的对象
  2. use that Clip object when the key is pressed in the input area 在输入区域中按下键时使用该Clip对象
  3. stop() and close() the Clip when the application is exiting or destroying the input area 当应用程序退出或破坏输入区域时,将stop()close() Clip

This would save the resources. 这样可以节省资源。

This is not a compiler error message. 这不是编译器错误信息。

It is a runtime error which I would assume means it could not read the data in the sound file. 我认为这是一个运行时错误,意味着它无法读取声音文件中的数据。 Either the file is corrupt or has a format which is not supported. 文件损坏或格式不受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM