gpt4 book ai didi

java - 如何摆脱错误?

转载 作者:行者123 更新时间:2023-12-03 02:29:56 25 4
gpt4 key购买 nike

我正在编写应该实际说出当前按下的键的文本编辑器。我设法做到了,我什至准备了文件。一切正常,但是当符号数量超过 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 接口(interface)的类。此类是我的主要文本区域的监听器。
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

最佳答案

您收到此错误是因为 Line 由于资源限制,不可用,根据描述的文档 here .

我认为你应该关闭 Line Line.close() 一起使用后方法。

编辑:
正确的做法可能如下:

  • 创建 Clip启动应用程序或创建用于处理输入区域的对象时的对象
  • 使用它Clip在输入区域中按下键时的对象
  • stop()close() Clip当应用程序退出或破坏输入区域时

  • 这样可以节省资源。

    关于java - 如何摆脱错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5789886/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com