2

I'm trying to get this program to loop the song infinitely, but I've only managed to get it working once. Any advice?

Here is my code:

public static void music(){

    String filename = "darkAura.wav";
    ContinuousAudioDataStream loop = null;
    InputStream in = null;
    try {
        in = new FileInputStream(filename);
    } catch (FileNotFoundException ex) {
        System.out.println("File not found");
    }
    try {
        AudioStream s = new AudioStream(in);
        AudioData MD;
        AudioPlayer.player.start(s);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }

}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Xenorosth
  • 97
  • 1
  • 9
  • Please have a look at [Looping audio on separate thread in Java](http://stackoverflow.com/questions/23255162/looping-audio-on-separate-thread-in-java/23255259#comment35586476_23255259) that is posted in the same context. Find the answer in the question itself. – Braj Apr 26 '14 at 00:26
  • Is the code in [this answer](http://stackoverflow.com/a/23250089/877472) of any use? – Paul Richter Apr 26 '14 at 02:15
  • No. This, after adjusting, simply makes the sound stop. The moment I use a timer, it doesn't work. – Xenorosth Apr 26 '14 at 02:18
  • 1
    Use the [Java Sound](http://stackoverflow.com/tags/javasound/info) based `Clip`. The linked page has an example of looping a sound. – Andrew Thompson Apr 26 '14 at 03:24

1 Answers1

2

You are not using loop variable at all. Try it like this:

AudioStream s = new AudioStream(in);     
AudioData audiodata = s.getData();
loop = new ContinuousAudioDataStream(audiodata);
AudioPlayer.player.start(loop);
vitro
  • 900
  • 6
  • 20
  • 1
    Where is `loop` declared? – Brian Tracy Apr 25 '14 at 23:49
  • In his code: `ContinuousAudioDataStream loop = null;` – vitro Apr 25 '14 at 23:50
  • The ContinuousAudioDataStream doesn't work. When I try the solution above, I get this: could not create AudioData object – Xenorosth Apr 26 '14 at 00:05
  • one possible workaround is to use official sound API as suggested here: http://stackoverflow.com/questions/7676723/could-not-create-audiodata-object or use JMF as suggested here: http://stackoverflow.com/questions/3274775/using-music-in-a-java-program Also playing with file size may help, see here: http://stackoverflow.com/questions/7676723/could-not-create-audiodata-object – vitro Apr 26 '14 at 00:25
  • So far, I've not been able to use any of them to loop the .wav file. I can't get the JMF import to work, I can't change the file size, and the API solution simply just didn't work. :/ Overall, I think I'm just going to give up on it. – Xenorosth Apr 26 '14 at 01:45
  • Braj's comment to your answer might do the job - if you have managed to play the audio once, perhaps looping it in a while loop will work, though there may be tiny gap on before the next repetition, you would have to try. Also for JMF to work you have to include proper libraries in your project, but for that you would have to consult other thread, perhaps [this one](http://stackoverflow.com/a/16163731/3356028) if using Eclipse IDE. – vitro Apr 26 '14 at 02:02
  • Braj's made it mute. And I've looked into JMF before it. Normally posting here is a last resort. I just can't seem to find the proper file, or if I have found it, how to set it up. :/ Suppose that might just because I'm a noobish college student. – Xenorosth Apr 26 '14 at 02:24