为游戏添加声音。需要帮助

时间:2016-01-31 02:05:45

标签: java audio

所以我有这个代码,我想知道如何将其与我的游戏的其余部分一起加入,因为它现在播放声音的唯一方法是选择此类作为启动器。此外,我可以得到一些信息,究竟是什么改变了,为什么。我觉得我应该更好地理解这段代码,因为我只是找到了代码并粘贴了它:D 代码:

package main;

import main.Handler;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;


public class AudioPlayer implements LineListener {

    boolean playCompleted;

    void play(String audioFilePath) {
        File audioFile = new File(audioFilePath);

        try {
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

            AudioFormat format = audioStream.getFormat();

            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip audioClip = (Clip) AudioSystem.getLine(info);

            audioClip.addLineListener(this);

            audioClip.open(audioStream);

            audioClip.start();

            while (!playCompleted) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            audioClip.close();

        } catch (UnsupportedAudioFileException ex) {
            System.out.println("The specified audio file is not supported.");
            ex.printStackTrace();
        } catch (LineUnavailableException ex) {
            System.out.println("Audio line for playing back is unavailable.");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Error playing the audio file.");
            ex.printStackTrace();
        }

    }


    public void update(LineEvent event) {
        LineEvent.Type type = event.getType();

        if (type == LineEvent.Type.START) {
            System.out.println("Playback started.");

        } else if (type == LineEvent.Type.STOP) {
            playCompleted = true;
            System.out.println("Playback completed.");
        }

    }

    public static void main(String[] args) {
        String audioFilePath = "res/music/dark_theme.wav";
        AudioPlayer player = new AudioPlayer();
        player.play(audioFilePath);
    }


    }'

1 个答案:

答案 0 :(得分:2)

这个类是一个相当普通的类:构造一个实例,调用一些方法,然后它做了预期的事情。当您将它用作启动器时,您所做的只是调用main方法,该方法用作如何使用此类的示例:

public static void main(String[] args) {
    String audioFilePath = "res/music/dark_theme.wav";
    AudioPlayer player = new AudioPlayer();
    player.play(audioFilePath);
}

只需构造一个实例,并使用所需音频文件的名称在其上调用play()

然而,请注意,由于以下几个原因,这门课程并不是一个很好的例子:

  • 它在播放时会阻止,这意味着您无法开始播放声音并同时执行不同的操作。
  • 在不引发问题的情况下,它不能发出多次声音。

因此,让我们修改此类以避免出现这些问题。这个类允许你将一个片段加载到内存中,并异步启动它(意味着你启动它然后你的程序继续运行)。 start()方法启动它播放一次,loop()方法永远循环它。 stop()是不言自明的,一旦你不再需要这个音频片段,就应该调用cleanup()来释放资源。 (当然,如果您打算很快再次开始播放剪辑,那么您不应该在那时进行清理())。

import main.Handler;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlayer{
    Clip audioClip;
    boolean playCompleted;
    String path;
    public AudioPlayer(String path){
        this.path = path;
        File audioFile = new File(path);

        try {
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

            AudioFormat format = audioStream.getFormat();

            DataLine.Info info = new DataLine.Info(Clip.class, format);

            audioClip = (Clip) AudioSystem.getLine(info);

            audioClip.open(audioStream);

        } catch (UnsupportedAudioFileException ex) {
            System.out.println("The specified audio file is not supported.");
            ex.printStackTrace();
        } catch (LineUnavailableException ex) {
            System.out.println("Audio line for playing back is unavailable.");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Error playing the audio file.");
            ex.printStackTrace();
        }
    }
    void play() {
        audioClip.start();

    }
    void loop(){
        audioClip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    void stop(){
        audioClip.stop();
    }
    void cleanup(){
        audioClip.close();
    }

    public static void main(String[] args) throws InterruptedException {
        String audioFilePath = "res/music/dark_theme.wav";
        AudioPlayer player = new AudioPlayer(audioFilePath);
        player.play();
        // give the sound time to play
        while(true){
            Thread.sleep(1000);
        }
    }


}