如何在Java 8中使用声音效果?蛇吃苹果

时间:2018-12-24 21:13:25

标签: java audio javasound

我写了Snake代码,想在蛇吃一个苹果时增加声音效果。我从YT的某个人那里复制了一个代码,但是对我来说不起作用。有人可以解释一下该怎么做吗?

代码:

import com.sun.tools.javac.Main;

import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;

public class AppleEatSoundEffect {
    public static Mixer mixer;
    public static Clip clip;

    public static void main(String[] args) {
        Mixer.Info[] mixInfos = AudioSystem.getMixerInfo();
        mixer = AudioSystem.getMixer(mixInfos[0]);

        DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
        try {
            clip = (Clip) mixer.getLine(dataInfo);
        } catch (LineUnavailableException lue) {
            lue.printStackTrace();
        }

        try {
            URL soundURL = Main.class.getResource("NotBad.wav");
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
            clip.open(audioStream);
        } catch (LineUnavailableException lue) {
            lue.printStackTrace();
        } catch (UnsupportedAudioFileException uafe) {
            uafe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

            clip.start();
            do {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            } while (clip.isActive());
        }
    }

编译器说clip = (Clip) mixer.getLine(dataInfo);出了点问题:

  

线程“ main”中的异常java.lang.IllegalArgumentException:不支持的行:接口Clip       在java.desktop / com.sun.media.sound.PortMixer.getLine(PortMixer.java:131)

1 个答案:

答案 0 :(得分:0)

以下是一种允许您播放音频文件(尤其是常见的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);