簡體   English   中英

Java中的聲音效果

[英]Sound effects in java

當我的太空飛船發射子彈時,我一直試圖獲得一種簡單的“ pe聲”。 聲音是一個.wav文件,已保存在我的netbeans項目的主文件夾中。

我在運行代碼時沒有任何錯誤,只是不會播放聲音。

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

聲音.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();
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM