user3779397
user3779397

Reputation: 11

Playing music continuously in the loop in java

I have written this code for playing music in my java program, but I want to play the music continuously in the loop, for that I have tried an infinite loop as well but it is not working. Please tell how it possible to play the music continuously?

import java.io.FileInputStream;
import sun.audio.*;

public class A {


    public static void main(String arg[]) throws Exception {       

        AudioPlayer MGP = AudioPlayer.player;
        AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
        AudioPlayer.player.start(BGM);
    }
}

Upvotes: 0

Views: 2329

Answers (1)

Erik Nedwidek
Erik Nedwidek

Reputation: 6184

From the JavaDoc:

To play a continuous sound you first have to create an AudioData instance and use it to construct a ContinuousAudioDataStream. For example:

 AudioData data = new AudioStream(url.openStream()).getData();
 ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
 AudioPlayer.player.start(audiostream);

I really didn't think it would be that difficult to adapt the docs.

import java.io.FileInputStream;
import sun.audio.*;

public class A {

    public static void main(String arg[]) throws Exception {    

        AudioData data = new AudioStream(new FileInputStream("yourfile.wav")).getData();
        ContinuousAudioDataStream BGM = new ContinuousAudioDataStream(data);
        AudioPlayer.player.start(BGM);
    }
}

Upvotes: 2

Related Questions