Sun*_*ahi 4 java javasound soundeffect
我正在制作一个基于Java的游戏,我想添加一些声音效果.我搜查了一下,发现自己更加困惑.我知道编码因文件格式而异.我只需要一些声音 - 无论哪种格式都无关紧要.所以请建议我最简单的文件格式.代码段非常有用.
什么是最简单的格式和提供音效的方法?
Bas*_*man 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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以随时更改"click.wav"以获取其他声音,包括.au文件.
正如mikera和Basilio德国人所说,AU和WAV都是游戏中使用的短音效的好选择.当前的JRE支持返回的类型AudioSystem.getAudioFileTypes(),但我会坚持使用其中的一个.
本Clip类提供简单的方法来加载和播放声音字节.
举个例子,这里是Java Sound信息的例子.页面.
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!");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19222 次 |
| 最近记录: |