Audio Clip won't loop continuously
Asked Answered
Q

3

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);
Quinnquinol answered 23/1, 2012 at 23:3 Comment(8)
Have you tried doing the loop() before open()? I have not tried it but it might work.Sofiasofie
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 ?Parkinson
Hi, I tried this too and it works. Do you have som exception after first loop ?Avertin
@DanW - Yeah putting loop() before open() just doesn't play anything.Quinnquinol
@Parkinson - No exceptions are being thrown.Quinnquinol
@Avertin - 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.Quinnquinol
@Frank - You are spot on. That is the answer.Quinnquinol
See also the "Playing a Clip" source on the JavaSound info. page, which creates A GUI element to prevent the Clip's daemon Thread from terminating at the end of the main() ;)Anyways
S
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
    }
}
Sim answered 23/1, 2012 at 23:32 Comment(1)
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.Quinnquinol
W
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.

Widner answered 31/7, 2013 at 3:41 Comment(0)
T
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.

Tehee answered 30/10, 2013 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.