简体   繁体   中英

What is wrong with my code so it can't play wav file?

I'm trying to use the code which available on: How can I play sound in Java? but I can't post question there since this is a new account and only have 1 reputation.

original code:

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

and this is my code:

package sound_test;
import javax.sound.sampled.*;

public class Main {

public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
  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();
}

public static void main(String[] args) {
    // TODO code application logic here
    playSound("C:\\warning_test.wav");
}

}

When I run the code i receive "null" as the output and no sound came out. I've checked the file name and the path, it's correct.

screenshots:

http://puu.sh/pkYo

http://puu.sh/pkZl

Thank you in advance.

you could do

AudioInputStream inputStream=AudioSystem.getAudioInputStream(new File(url));

also add a delay after click.start(); ie Thread.Sleep(4000); click.start(); ie Thread.Sleep(4000);

or if you want to make sure it plays the entire audio sample you could use a simple snippet such as

import javax.sound.sampled.*;
import java.io.File;

public class Main  implements LineListener {
private boolean done = false;
public  void update(LineEvent event) {
    if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE) {
      done = true;
    }
}

public void waitonfinish() throws InterruptedException {
   while(!done) {
       Thread.sleep(1000);
   } 
}
public static  void playSound(final String url) {

    try {
      Clip clip = AudioSystem.getClip();
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
      Main control = new Main();
      clip.addLineListener(control);
      clip.open(inputStream);
      clip.start();
      control.waitonfinish();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }

public static void main(String[] args) {
    // TODO code application logic here
    playSound("C:\\warning_test.wav");   
 }
}

`

You copied the code entirely without noticing the in the original, it points to

path/to/sounds

since you give it the full path, u should replace it with just url:

 AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream(url));

EDIT: I tried here and got null as well. I changed it to create the audioInput from a file:

import java.io.File;

import javax.sound.sampled.*;

public class Main {

    public static synchronized void playSound(final File file) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Clip clip = AudioSystem.getClip();
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
                    clip.open(inputStream);
                    clip.start();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println(e.getMessage());
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        File file = new File("C:\\warning_test.wav");
        playSound(file);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM