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:
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.JComponen开发者_如何学编程t.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. 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? Maybe it's because I have to close the clip by clip.close()
after clip.start()
? Please, help :-S
You get this error because the Line
is not available due to resource restrictions, according to the documentations described here.
I think you should close the Line
after you used it with the Line.close()
method.
EDIT: The proper way of doing it could be the following:
- create a
Clip
object when starting the application or creating objects for handling the input area - use that
Clip
object when the key is pressed in the input area stop()
andclose()
theClip
when the application is exiting or destroying the input area
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.
精彩评论