2

I am trying to open a .wav file and play it using a Clip. But when I call myClip.open(...), the thread freezes and never resumes. No errors are thrown. Here is a simple version of my code:

try {
    AudioInputStream mySound = AudioSystem.getAudioInputStream(new File("sounds/myAudioFile.wav"));
    myClip = AudioSystem.getClip();
    myClip.open(mySound); //This is where it hangs
    myClip.start(); //This is never executed
} catch (Exception e) {e.printStackTrace();}

EDIT: An alternate version of my code (also doesn't work):

Clip myClip = null;

new Thread(new Runnable() {
     public void run() {
          try {
              AudioInputStream mySound = AudioSystem.getAudioInputStream(new File("sounds/myAudioFile.wav"));
              myClip = AudioSystem.getClip();
              System.out.println("Before open");
              myClip.open(mySound); //This is where it hangs
              System.out.println("After open"); //never executed
              //This thread runs indefinitely, whether or not clip.start() is called
          } catch (Exception e) {e.printStackTrace();}
     }
}).start();

try{  Thread.sleep(1000);  }catch(Exception e){} //give it a second to open

myClip.start(); //Executed, but doesn't make a sound
System.out.println("Started clip");

Output:

Before open
Started clip

I know what causes this, but I can't figure out a way the thread from freezing eternally. It freezes because the sound drivers on my computer occasionally stop working, and prevent any sound from any program from playing. I just need some way to force the clip.open method (or thread) to timeout after about 2 to 3 seconds. Calling clip.start() in another thread works, but plays no sound because the clip hasn't opened. And the thread containing clip.open(...) runs forever, even after calling clip.start().

Daniel Williams
  • 635
  • 7
  • 14
  • Try to write it in a thread. Otherwise it will block your main thread, which feels like freezing. – Boola Jun 21 '16 at 06:14

1 Answers1

0
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.getAudioInputStream(new File("sounds/myAudioFile.wav"));       // you can pass it in url
        clip.open(inputStream);
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }
  }).start();
}

One more point: You haven't started your clip. use clip.start() In this case, you won't have to use different thread as clip.start() spawns a new thread itself.

Boola
  • 358
  • 3
  • 14
  • That does fix my freezing problem, but it also creates a new thread that will never end. I suppose that isn't a problem, right? – Daniel Williams Jun 21 '16 at 06:52
  • I wrote already a way to avoid that. As soon as you use clip.start(), it will spawn a new thread. actual problem in your code is you are just opening the clip but never starting it. so it waits for clip to start. – Boola Jun 21 '16 at 06:56
  • May be this can help :(though I am not sure it will solve your problem or not): http://stackoverflow.com/questions/5241822/is-it-good-way-to-stop-java-thread-forcefully – Boola Jun 22 '16 at 07:17