Java Sound .WAV格式

时间:2016-01-28 18:26:39

标签: java audio wav clip

我知道这个问题现在已被问过几次关于声音的问题了,相信我,我已经看了一下试图找出自己的答案。我之前在一个不同的项目中使用Java的声音没有任何困难使用它:

public void playWelcome (){

  try{

    InputStream inputStream = 

    getClass().getResourceAsStream("Start.wav");

    AudioStream audioStream = new AudioStream(inputStream);

    AudioPlayer.player.start(audioStream);

  }catch(IOException ioException){

    ioException.printStackTrace();

    System.out.println("Unable to Find WAV/Start FIle");
  }   
} 

显然,这在解释之前没有用,所以我试图使用:

public void uploadDownload() {

  try {
    AudioInputStream audioInputStream = 

    AudioSystem.getAudioInputStream(new

    File("/DaWord/src/resources/upload.wav").getAbsoluteFile());

    Clip clip = AudioSystem.getClip();

    clip.open(audioInputStream); 

    clip.start();

  } catch(Exception ex) {

    System.out.println("Error with playing sound.");

    ex.printStackTrace();
  }
}

注意:我也尝试过没有斜杠的文件。例如'my file.wav' 有人可以帮我一点吗?

更新:

所以我已经成功了......等等:

文件soundFile = new File(“/ Users / myname / DaWord / src / testing.wav”);

      AudioInputStream audioIn = 

AudioSystem.getAudioInputStream(音效档);

     // Get a sound clip resource.
     Clip clip = AudioSystem.getClip();

     // Open audio clip and load samples from the audio input stream.
     clip.open(audioIn);

     clip.start();

但有没有人知道如何通过不提供绝对路径来做到这一点?我试过使用.absoulutePath();在文件的末尾,例如:文件

file = new File(“sound.wav”)。absouloutePath();

2 个答案:

答案 0 :(得分:0)

正如here所解释的,这是从文件或URL分配管道的AudioInputStream的方法,:

// from a wave File
File soundFile = new File("eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
// from a URL
URL url = new URL("http://www.zzz.com/eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// can read from a disk file and also a file contained inside a JAR (used for distribution)
// recommended
URL url = this.getClass().getClassLoader().getResource("eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);

答案 1 :(得分:0)

这暂时有效......有完整路径

文件soundFile = new File(“/ Users / mrBerns / NetBeansProjects / DaWord / src / ching.wav”);

      AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
     // Get a sound clip resource.
     Clip clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioIn);
     clip.start();