如何在Java8中使用音效?蛇吃苹果

如何在Java8中使用音效?蛇吃苹果,java,audio,javasound,Java,Audio,Javasound,我编写了Snake代码,想在Snake吃苹果时添加一个声音效果。我从YT上的某个人那里复制了一个代码,但对我不起作用。有人能告诉我怎么做吗 代码: 编译器说clip=clip mixer.getLinedataInfo;有问题: 主线程java.lang.IllegalArgumentException中出现异常:不支持的行:接口剪辑 位于java.desktop/com.sun.media.sound.PortMixer.getLinePortMixer.java:131 下面是一种允许您播放

我编写了Snake代码,想在Snake吃苹果时添加一个声音效果。我从YT上的某个人那里复制了一个代码,但对我不起作用。有人能告诉我怎么做吗

代码:

编译器说clip=clip mixer.getLinedataInfo;有问题:

主线程java.lang.IllegalArgumentException中出现异常:不支持的行:接口剪辑 位于java.desktop/com.sun.media.sound.PortMixer.getLinePortMixer.java:131


下面是一种允许您播放音频文件的方法,特别是常见的WAV或MIDI文件。试试看…如果你得到你想要的音频,请告诉我

public Thread playWav(final File wavFile, final boolean... loopContinuous) {
    String ls = System.lineSeparator();
    // Was a file object supplied?
    if (wavFile == null) {
        throw new IllegalArgumentException(ls + "playWav() Method Error! "
                + "Sound file object can not be null!" + ls);
    }

    // Does the file exist?
    if (!wavFile.exists()) {
        throw new IllegalArgumentException(ls + "playWav() Method Error! "
                + "The sound file specified below can not be found!" + ls
                + "(" + wavFile.getAbsolutePath() + ")" + ls);
    }

    // Play the Wav file from its own Thread.
    Thread t = null;
    try {
        t = new Thread("Audio Thread") {
            @Override
            public void run() {
                try {
                    Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
                    audioClip = clip;
                    clip.addLineListener((LineEvent event) -> {
                        if (event.getType() == LineEvent.Type.STOP) {
                            clip.drain();
                            clip.flush();
                            clip.close();
                        }
                    });
                    clip.open(AudioSystem.getAudioInputStream(wavFile));
                    // Are we to loop the audio?
                    if (loopContinuous.length > 0 && loopContinuous[0]) {
                        clip.loop(Clip.LOOP_CONTINUOUSLY);
                    }
                    clip.start();
                }
                catch (LineUnavailableException | UnsupportedAudioFileException | IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        t.start();
        Thread.sleep(100);
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    return t;
}
要使用此方法,只需随时随地调用它即可播放特定的音效:

File file = new File("resources/NotBad.wav");
playWav(file);
确保文件对象指向正确的文件位置。如果您想将音频文件循环作为游戏背景音乐,则为可选的loopContinuous参数提供布尔值true:

File file = new File("resources/BackgroundMusic.mid");
playWav(file, true);

为什么要导入com.sun.tools.javac.Main?TagWiki有一个设置剪辑的正确示例。老实说,YouTube上的代码看起来像垃圾。我不知道他们认为他们在用mixInfos[0]做什么。你每天都能学到新东西。开玩笑的@shmosel这根本不应该在这里。@shomel idk,我对这些东西真的很陌生:真的很新你是怎么处理@Radiodef链接的代码的?
File file = new File("resources/BackgroundMusic.mid");
playWav(file, true);