2012-05-18 29 views
4

我正在製作一個基於Java的遊戲,我想添加一些聲音效果。我搜查了一下,發現自己更加困惑。我知道編碼不同於文件格式和格式。 我只需要一些聲音 - 無所謂格式。所以請給我建議最簡單的文件格式。代碼片段將非常有幫助。我應該使用什麼音頻格式的Java?

什麼是最簡單的格式和提供音效的方式?

回答

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文件。

3

音頻的文件格式都取決於你需要什麼。 MIDI文件使用音符等,因此適用於簡單的背景音樂,但是MIDI文件無法制作特定的聲音,如爆炸,槍聲,不管它是什麼。把MIDI看作是樂器的音樂,它不能用於音效。這是WAV或MP3文件的起源地,因爲它們以聲音的形式播放聲音,可以真正播放任何聲音。它們是較大的文件(WAV未壓縮,因此甚至更大),但它們經常用於聲音效果。還有其他格式可供使用,只需搜索即可。你不應該把聲音格式放在最簡單的使用方式上,這取決於你使用的是什麼(音樂和音效等)。

4

Java支持相當廣泛的聲音格式。

如果你只是使用基本的Java API的聲音(這是罰款基本效果),那麼下面可能是從Oracle常見問題有所幫助:

哪些音頻格式的Java聲音支持?
Java Sound支持以下音頻文件格式:AIFF,AU和WAV。 它還支持以下基於MIDI的歌曲文件格式:SMF類型0 (標準MIDI文件,又名.mid文件),SMF類型1和RMF。

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

8

作爲mikera &巴西利奧德語說明,AU & 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!"); 
      } 
     }); 
    } 
} 
+0

非常感謝您的工作,我會學習它.. –

0

您可以使用剪輯類,並使用.wav和.AU的小短的聲音效果。如果您嘗試播放mp3音樂,但您始終可以使用java java這樣的mp3 Java庫。

相關問題