Sound clip will not work cyclically

Can someone point me in the right direction why this code will not play this audio clip continuously? He plays it once and stops.

final Clip clip = AudioSystem.getClip(); final AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("Alarm_Police.wav")); clip.open(inputStream); clip.loop(Clip.LOOP_CONTINUOUSLY); 
+7
source share
3 answers

If you are using a larger application, this answer may not apply. But for a simple test, only with this piece of code this can help:

Clip.loop () starts its own thread, but this thread does not support JVM. Therefore, to make it work, make sure that the clip is not the only thread.

If I left Thread.sleep (..) from this fragment, I get the same problem as you;

 import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; public class Snippet { public static void main(String[] args) throws Exception { AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("notify.wav")); Clip clip = AudioSystem.getClip(); clip.open(inputStream); clip.loop(Clip.LOOP_CONTINUOUSLY); Thread.sleep(10000); // looping as long as this thread is alive } } 
+12
source

To play a complete audio file, you need to know how much time you need to sleep. An alternative could be:

 while(clip.isRunning()) { Thread.sleep(100); } 

This allows you to sleep (in 100 ms increments) until the isRunning () state becomes false. You may need an initial sleep before this loop to set the isRunning () condition.

+1
source

My audio file contains 20 seconds of beep. I need him to call. Instead of using the stream, I continued with the code snippet below.

  while(true){ clip.start(); clip.loop(clip.LOOP_CONTINUOUSLY); } 

I think this will help. Thanks.

+1
source

All Articles