9

Can anyone point me in the right direction as to why this code will not play this audio clip continuously? It 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);
daveed007
  • 168
  • 1
  • 1
  • 7
  • Have you tried doing the loop() before open()? I have not tried it but it might work. – Dan W Jan 23 '12 at 23:11
  • I just tried it here and it seems to work... are you catching any exception in the try block that should be surronding this code ? – Timst Jan 23 '12 at 23:16
  • Hi, I tried this too and it works. Do you have som exception after first loop ? – hudi Jan 23 '12 at 23:19
  • @DanW - Yeah putting loop() before open() just doesn't play anything. – daveed007 Jan 24 '12 at 01:46
  • @Timst - No exceptions are being thrown. – daveed007 Jan 24 '12 at 01:48
  • @hudi - When you say it works, do you mean it plays the file? I get that too, my problem is that it doesn't keep looping infinitely. – daveed007 Jan 24 '12 at 01:48
  • @Frank - You are spot on. That is the answer. – daveed007 Jan 24 '12 at 01:48
  • 1
    See also the "Playing a `Clip`" source on the [JavaSound info. page](http://stackoverflow.com/tags/javasound/info), which creates `A GUI element to prevent the Clip's daemon Thread from terminating at the end of the main()` ;) – Andrew Thompson Jan 24 '12 at 02:27

3 Answers3

14

If you are running a bigger application, this answer may not apply. But for a simple test with only that piece of code, this may help:

Clip.loop() starts it's own thread, but that thread will not keep the JVM alive. So to make it work, make sure the clip is not the only thread.

If I leave out Thread.sleep(..) from this snippet, I get the same issue 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
    }
}
Paaske
  • 4,345
  • 1
  • 21
  • 33
  • 2
    Thanks Frank, as far as I understand if I hit the up arrow key it adds to your 'reputation' or something, but I don't have enough posts to allow me to do so. I hit the green checkmark, which I hope means I choose your answer to answer the question. – daveed007 Jan 24 '12 at 01:50
1

In order to play the complete audio file you would need to know the length of time to sleep. An alternative would be to:

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

This keeps sleeping (in 100ms increments) until the isRunning() state has turned to false. You may need an initial sleep before this loop so that the isRunning() state has time to set.

Lord Ashes
  • 11
  • 1
1

My audio file contains 20 seconds of an alarm beep. I need it continuously to ring. Instead of using thread, i went on with the piece of code shown below.

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

Think this would help. Thanks.

Firnaz
  • 553
  • 7
  • 31