gpt4 book ai didi

java - 在java中加载音频时出错(在接口(interface)Clip中非法调用open())

转载 作者:行者123 更新时间:2023-11-30 02:58:33 27 4
gpt4 key购买 nike

我正在为我的游戏编写一个新的音频系统,我遇到了这个错误,并且似乎无法在任何地方找到和解决方案,

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)

这是我用来加载和播放音频的代码。

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 来播放。

Line#open :

IllegalArgumentException - if this method is called on a Clip instance.

您需要调用clip.open(audioStream)而不是clip.open()。另外,您需要在启动 Clip 之前执行此操作。

关于java - 在java中加载音频时出错(在接口(interface)Clip中非法调用open()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36536144/

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