java中的声音效果

时间:2013-12-03 14:58:51

标签: java audio netbeans

当我的太空船发射子弹时,我一直试图得到一个简单的“皮尤”声。声音是我保存在netbeans项目主文件夹中的.wav文件。

运行代码时没有任何错误,它只是不会播放声音。

在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();
    }
}

我终于开始工作了。这就是我所做的。

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

稍后在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();
}

1 个答案:

答案 0 :(得分:1)

您可以使用此方法播放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();
}