1

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.

Community
  • 1
  • 1
chiaki_kei
  • 43
  • 1
  • 7
  • try `AudioInputStream inputStream =AudioSystem.getAudioInputStream(Main.class.getResourceAsStream(url))` – keety Apr 13 '12 at 04:55
  • tried it, no difference, thx for the help anyway :) – chiaki_kei Apr 13 '12 at 05:23
  • This question really has little or nothing to do with JavaSound and everything to do with locating resources. To test that theory, try the 1st source shown on the [JavaSound info. page](http://stackoverflow.com/tags/javasound/info). Does it work for you? – Andrew Thompson Apr 13 '12 at 07:38
  • i didn't put the javasound tag.. just realized it now, it's removed now thank you for noticing :) – chiaki_kei Apr 13 '12 at 10:02
  • Here are the similar threads from stackoverflow only... These thread should help you http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java http://stackoverflow.com/questions/2416935/how-to-play-wav-files-with- – Dhruv Apr 13 '12 at 05:03

2 Answers2

0

you could do

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

also add a delay after click.start(); i.e 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");   
 }
}

`

keety
  • 17,231
  • 4
  • 51
  • 56
0

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);
    }
La bla bla
  • 8,558
  • 13
  • 60
  • 109