在java中加载音频时出错(在接口剪辑中非法调用open())

在java中加载音频时出错(在接口剪辑中非法调用open()),java,audio,javasound,Java,Audio,Javasound,我正在为我的游戏编写一个新的音频系统,我遇到了这个错误,似乎在任何地方都找不到解决方案 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

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

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()


有人能帮我吗?

你不能把任何东西传给
剪辑来播放

:

IllegalArgumentException
-如果在
Clip
实例上调用此方法

您需要调用
clip.open(audioStream)
而不是
clip.open()
。此外,您需要在开始
剪辑之前执行此操作