I'm trying to make an audio play again after it finishes.
Libraries I use:
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
I use while loop, boolean and sleep():
boolean play = true;
while(play) {
playMusic("C:\\Users\\Ученик\\IdeaProjects\\Game\\src\\com\\company\\audio\\skeleton.wav");
Thread.sleep(10);
Here is the function:
public static void playMusic(String filepath) {
String reset = "\u001B[0m";
String red = "\u001B[31m";
//->
InputStream music;
try {
music = new FileInputStream(new File(filepath));
AudioStream audios = new AudioStream(music);
boolean game = true;
while(true)
AudioPlayer.player.start(audios);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error. Can't find an audiofile skeleton.wav");
System.out.println(red + "Error. Can't find an audiofile skeleton.wav" + reset);
}
}
But when the audio finishes, there is silence.
It doesn't play again in 10 ms.. Why?
How to make repeated playback of audio?