0

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?

codor01
  • 1
  • 2
  • You should be using a dedicated thread to do that . And you need to loop all the code, not just the play method. Have a look at `SwingWorker` to provide your thread – g00se Dec 05 '22 at 11:10

1 Answers1

0

Please don't use classes under sun.*, see this. For proper audio playing, see this.

However you should do something like this to achieve what you want:

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.io.File;

public class Audio {

    public static void main(String[] args) {
        Clip clip = getClip("C:\\Users\\Ученик\\IdeaProjects\\Game\\src\\com\\company\\audio\\skeleton.wav");
        playForever(clip);
        boolean play = true;
        // ...
        // do stuff
        // ...
        if (!play) clip.stop();
    }

    public static void playForever(Clip clip) {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        clip.start();
    }

    public static Clip getClip(String filepath) {
        String reset = "\u001B[0m";
        String red = "\u001B[31m";
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new File(filepath)));
            return clip;
        } 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);
            throw new RuntimeException(e);
        }
    }

}

To play the audio even when the main thread finishes, you should do this:

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.io.File;

public class Audio {

    public static void main(String[] args) {
        Clip clip = getClip("C:\\Users\\Ученик\\IdeaProjects\\Game\\src\\com\\company\\audio\\skeleton.wav");
        Thread thread = playForever(clip);
        // ...
        // do stuff
        // ...
        boolean play = true;
        if (!play) stopAudio(thread);
    }

    public static Thread playForever(Clip clip) {
        Thread thread = new Thread(() -> {
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            clip.start();
            synchronized (Thread.currentThread()) {
                try {
                    Thread.currentThread().wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        return thread;
    }

    public static void stopAudio(Thread thread) {
        synchronized (thread) {
            thread.notify();
        }
    }

    public static Clip getClip(String filepath) {
        String reset = "\u001B[0m";
        String red = "\u001B[31m";
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new File(filepath)));
            return clip;
        } 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);
            throw new RuntimeException(e);
        }
    }

}
Al-Anazi
  • 364
  • 1
  • 8