java应该使用什么音频格式?

java应该使用什么音频格式?,java,javasound,soundeffect,Java,Javasound,Soundeffect,我正在制作一个基于Java的游戏,我想为它添加一些音效。我搜索了一下,发现自己更加困惑了。我知道不同格式的文件编码不同。 我只需要一些声音-不管是哪种格式。所以请建议我最简单的文件格式。代码片段将非常有用 提供音效最简单的格式和方式是什么 音频文件格式都取决于您需要什么。 MIDI文件使用音符等,因此适合于简单的背景音乐,但MIDI文件无法发出爆炸、枪声等特定声音。可以把MIDI看作是乐器的音乐,它不用于音效。这是WAV或MP3文件开始播放的地方,因为它们以波的形式播放声音,所以可以真正播放任何

我正在制作一个基于Java的游戏,我想为它添加一些音效。我搜索了一下,发现自己更加困惑了。我知道不同格式的文件编码不同。 我只需要一些声音-不管是哪种格式。所以请建议我最简单的文件格式。代码片段将非常有用


提供音效最简单的格式和方式是什么

音频文件格式都取决于您需要什么。
MIDI文件使用音符等,因此适合于简单的背景音乐,但MIDI文件无法发出爆炸、枪声等特定声音。可以把MIDI看作是乐器的音乐,它不用于音效。这是WAV或MP3文件开始播放的地方,因为它们以波的形式播放声音,所以可以真正播放任何声音。它们是较大的文件(WAV未压缩,因此更大),但它们通常用于声音效果。还有其他格式可供使用,只需四处搜索即可。您不应该将声音格式建立在最容易使用的基础上,它实际上取决于您使用它的确切目的(音乐与声音效果等)。

Java支持非常广泛的声音格式

如果您只是使用基本Java Sound API(这对于基本效果很好),那么Oracle常见问题解答中的以下内容可能会有所帮助:

Java声音支持哪些音频格式
Java声音支持以下音频文件格式:AIFF、AU和WAV。 它还支持以下基于MIDI的歌曲文件格式:SMF类型0 (标准MIDI文件,又称.mid文件)、SMF类型1和RMF


我已经成功地使用了AU和WAV。

对于短声音,您应该使用WAV或AU,WAV是最常见的小声音格式。我刚刚完成了这个小程序,你所需要做的就是有一个.wav的声音

此程序生成一个带有按钮的窗口,每次单击该按钮时,都会播放指定的声音

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class PlaySound extends JFrame{

    private Clip clip;

    public static void main(String [] args) {

        PlaySound app = new PlaySound();

    }

    public PlaySound() {
        JButton play = new JButton("Play");//here we make the button
        play.addActionListener(new ActionListener() {//here we tell what the button will do
        public void actionPerformed(ActionEvent e) {
            playTheSound();//when its clicked call this method
        }
    });

    this.add(play);
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

private void SoundEffect(URL url) {
    try {
        // Set up an audio input stream piped from the sound file.
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
        // Get a clip resource.
        clip = AudioSystem.getClip();
        // Open audio clip and load samples from the audio input stream.
        clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}

// Play or Re-play the sound effect from the beginning, by rewinding.
public void playTheSound() {

    URL url = getClass().getResource("click.wav");//You can change this to whatever other sound you have
    SoundEffect(url);//this method will load the sound

    if (clip.isRunning())
        clip.stop();   // Stop the player if it is still running
    clip.setFramePosition(0); // rewind to the beginning
    clip.start();     // Start playing

    }

}

您可以随时更改“click.wav”以获得另一种声音,包括.au文件。

正如mikera&Basilio German指出的,对于游戏中使用的短音效,au和wav都是不错的选择。当前的JRE支持返回的类型,但我坚持使用这两种类型中的一种

该类提供了加载和播放声音字节的简单方法

关于一个例子,这里是来自的例子


您可以使用Clip类并使用.Wav或.Au来获得小的短音效果。但是,如果您试图播放mp3音乐,则始终可以使用类似javazoom的mp3 java库

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        // loop continuously
        clip.loop(-1);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}