本文介绍了在Java中加载音频时出错(接口Clip中对open()的非法调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为自己的游戏编写新的音频系统,但遇到此错误,似乎无法在任何地方找到解决方法,
I'm writing an new audio system for my game and i have come across this error and can not seem to find and solution anywhere,
java.lang.IllegalArgumentException: illegal call to open() in interface Clip
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
这是我用来加载和播放音频的代码.
This is the code i use to load and play my audio.
private Clip load(String filename) {
try {
//Loads the file
InputStream in = new FileInputStream(new File("res/" + filename + FILE_EXT));
//Create the input buffer
InputStream bufferedIn = new BufferedInputStream(in);
//Convert into an audio stream
AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);
//Get the audio format
AudioFormat format = audioStream.getFormat();
//Get the data line info
DataLine.Info info = new DataLine.Info(Clip.class, format);
//Return the clip
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
return this.clip = audioClip;
} catch (FileNotFoundException e) {
System.err.println("Failed to load audio! " + filename + " not found!");
throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) {
System.err.println("Failed to load audio! " + filename + " is unsupported!");
throw new RuntimeException(e);
} catch (IOException e) {
System.err.println("Failed to load audio! " + filename + " caused an IO Exception!");
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
System.err.println("Failed to load audio! " + filename + " line is unavalible!");
e.printStackTrace();
}
throw new RuntimeException("Failed to load audio! input == null!");
}
private void startClip() {
if(this.clip != null) this.clip.start();
else throw new RuntimeException("Failed to start audio clip! The clip appears to be null.");
}
private void stopClip() {
if(this.clip != null) this.clip.close();
else throw new RuntimeException("Failed to close audio clip! The clip appears to be null.");
}
@Override
public void play() {
try {
if(isPlaying()) return;
else {
startClip();
this.clip.open();
this.playing = true;
}
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
错误发生在this.clip.open()
;
有人可以帮助我吗?
推荐答案
您不会将任何内容传递给Clip
进行播放.
You don't pass anything to the Clip
to play.
您需要呼叫clip.open(audioStream)
而不是clip.open()
.另外,您需要在 开始Clip
之前执行此操作.
You need to call clip.open(audioStream)
instead of clip.open()
. Also, you need to do this before starting the Clip
.
这篇关于在Java中加载音频时出错(接口Clip中对open()的非法调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!