java中的音效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20354508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sound effects in java
提问by RaineShadow
I have been trying to get a simple "pew" noise for when my space ship shoots a bullet. The sound is a .wav file that I have saved in the main folder for my netbeans project.
当我的太空船发射子弹时,我一直试图获得一种简单的“pew”噪音。声音是一个 .wav 文件,我保存在我的 netbeans 项目的主文件夹中。
I don't get any errors when I run the code, it just won't play the sound.
运行代码时我没有收到任何错误,只是不会播放声音。
In Main.java when the mouse is pressed it shoots a bullet and should play the sound. Sound.java is found below.
在 Main.java 中,当按下鼠标时,它会发射子弹并播放声音。Sound.java 在下面找到。
Main.java
主程序
@Override
public void mousePressed(MouseEvent me) {
Color color = Color.yellow;
if(tankMissile == null){
tankMissile = new Missile(launcher.getXofMissileShoot(), launcher.getYofMissileShoot(), color);
tankMissile.setTarget(launcher.x, 0);
int size = 2;
tankMissile.setExplosionMaxSize(size);
synchronized (gameData.figures) {
gameData.figures.add(tankMissile);
}
}
new Sound("pew.wav").start();
}
Sound.java
声音文件
//www.anyexample.com/programming/java/java_play_wav_sound_file.xml
import java.io.*;
import javax.sound.sampled.*;
public class Sound extends Thread{
private String fileName;
public Sound(String wavfile){
fileName = wavfile;
}
@Override
public void run(){
File soundFile = new File(fileName);
if(!soundFile.exists()){
System.err.println("Wave file not found");
}
AudioInputStream audioInputStream = null;
try{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch(UnsupportedAudioFileException e){
e.printStackTrace();
return;
}
catch(IOException e){
e.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try{
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
}
catch(LineUnavailableException e){
e.printStackTrace();
return;
}
catch(Exception e){
e.printStackTrace();
return;
}
if(auline.isControlSupported(FloatControl.Type.PAN)){
FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
}
auline.start();
}
}
I got it to work finally. This is what I did.
我终于让它工作了。这就是我所做的。
Main.java
主程序
@Override
public void mousePressed(MouseEvent me) {
Color color = Color.yellow;
if(tankMissile == null){
tankMissile = new Missile(launcher.getXofMissileShoot(), launcher.getYofMissileShoot(), color);
tankMissile.setTarget(launcher.x, 0);
int size = 2;
tankMissile.setExplosionMaxSize(size);
synchronized (gameData.figures) {
gameData.figures.add(tankMissile);
}
try {
playSound("pew.wav");
} catch (MalformedURLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (LineUnavailableException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedAudioFileException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
A little later on in Main.java
稍后在 Main.java 中
public static void playSound(String fileName) throws MalformedURLException, LineUnavailableException, UnsupportedAudioFileException, IOException{
File url = new File(fileName);
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.start();
}
采纳答案by Batuhan B
You can use this method for playing a wav format. You should edit little and mix with your code.
您可以使用此方法播放 wav 格式。您应该很少编辑并与您的代码混合。
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}