简体   繁体   中英

How does one loop audio files in Java?

This is my code

public static void main(String[] args) {
    try{
        // open the sound file as a Java input stream
        InputStream in = new FileInputStream("Sound.wav");

        // create an audiostream from the inputstream
        AudioStream audioStream = new AudioStream(in);

        // play the audio clip with the audioplayer class
        AudioPlayer.player.start(audioStream); 
    }
    catch(Exception e){
        System.out.println("Audio not found");
    }
}

Sun Audio is Undocumented

You should not be using Sun Audio in your program. See here and here .

The Answer Depends on Your Library

If you are using Java Clip (built-in), try the example here . This is probably the simplest method.

If you switch to use JavaFX, you can see the API here , which has ways to loop. However, JavaFX requires a bit of package restructuring, and there is a learning curve. Unless you need JavaFX for other reasons, I would avoid it.

If you are using any other library, please consult the API for that library, or share with us what you are attempting to use. I have personally used BasicPlayer , but it is quite old.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM