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

时间:2012-05-18 01:26:02

标签: java javasound soundeffect

我正在制作一款基于Java的游戏,我想添加一些音效。我搜查了一下,发现自己更加困惑。我知道编码因文件格式而异。 我只需要一些声音 - 无论哪种格式都无关紧要。所以请建议我最简单的文件格式。代码段非常有用。

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

5 个答案:

答案 0 :(得分:12)

对于短音,你应该使用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文件。

答案 1 :(得分:8)

作为迈克拉& Basilio德国音符,AU& amp;对于游戏中使用的短音效,WAV是很好的选择。当前的JRE支持AudioSystem.getAudioFileTypes()返回的类型,但我会坚持使用其中的一个。

Clip类提供了加载和播放声音字节的简便方法。

举个例子,这是Java Sound info. page的例子。

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!");
            }
        });
    }
}

答案 2 :(得分:4)

Java支持各种各样的声音格式。

如果您只是使用基本的Java Sound API(适用于基本效果),那么以下内容可能有助于Oracle FAQ:

  

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

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

答案 3 :(得分:3)

音频的文件格式完全取决于您的需要。 MIDI文件使用音符等,因此适用于简单的背景音乐,但MIDI文件不能发出特定的声音,如爆炸,枪声,无论它是什么。将MIDI视为乐器的音乐,它不用于音效。这就是WAV或MP3文件发挥作用的地方,因为它们可以播放声音,因为它们可以播放任何声音。它们是较大的文件(WAV未压缩甚至更大)但它们经常用于声音效果。还可以使用其他格式,只需搜索。你不应该把声音格式建立在最容易使用的东西上,它实际上取决于你使用它的确切原因(音乐与声音效果等)。

答案 4 :(得分:0)

您可以使用Clip类并使用.Wav或.Au来获得小的短音效。如果你正在尝试播放mp3音乐,你可以随时使用像javazoom这样的mp3 java库。