1

Problem description: I have an array of sounds files with different length. Every time the Play button is clicked, I want one sound be played. The problem is that when the Play button is clicked twice (or more times), the next sound starts and overlaps the currently playing sound. I don't want to clip.stop()the current sound, I want the next sound not to start until the current sound is finished. I have tried different approaches, like boolean flag, clickCount and checking the Clip.isRunning();status, but they not really getting me to where I want. I also saw someone else having similar issue in OS (with MultiPlayer() though), but his problem was resolved AFAIK. Any suggestion would be appreciated!

Here is related code:

public class PlaySounds {
private AudioInputStream audioStream;
public static Clip clip;
private URL url;
public static int nextSound;

public void playAllSounds() {
    for (String str : soundList) { // arrayList of sounds
        str = soundList.get(nextSound);
        url = getClass().getResource(str);
    }
    try {

        audioStream = AudioSystem.getAudioInputStream(url);
        clip = AudioSystem.getClip();
        clip.open(audioStream);
        clip.start();

    } catch (Exception e) {
    }

 }
}

Main class file:

PlaySounds ps = new PlaySounds();
int next = 0;
List<Integer> positions = new ArrayList<>();
 /*Some other methods....
....
*/
    private void getshuffledPositions() {

    for (int i = 0, i <=12; i++) {
        positions.add(i);

    }
    Collections.shuffle(positions);

}
public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == playButton) {
        //Some codes.....Here I tried boolean flag, etc
        ps.nextSound = positions.get(next);
        ps.playAllSounds();
        //Some more codes.....
sumu
  • 11
  • 1

0 Answers0