How to play sounds sequentially?

edited February 2014 in How To...

I use minim to play sounds and it works. But it plays asynchronously. How do I play sounds sequentially?

Tagged:

Answers

  • im not that good at english, so you want to play more than one sound one after another? i think you can check with playing() if a sound is playing at the moment but i'm not sure!

    maybe i can help you a bit with my answer

  • I don't want to do a busy wait, I want to sleep until the sound is done playing. This code will play two sounds simultaneously.

    import ddf.minim.*;
    Minim minim;
    
    void say(String w) {
      AudioPlayer p = audioClips.get(w);
      if (p == null) {
        println("Loading: " + w);
        p = minim.loadFile(w);
        audioClips.put(w, p);
      }
      p.rewind();
      p.play();
    }
    
    void setup() {
        minim = new Minim(this);
        say("test1.mp3");
        say("test2.mp3");
    }
    
  • edited February 2014 Answer ✓

    It's not normally a good idea to completely halt the execution of a sketch (especially in Processing's graphics-oriented world), but in this isolated case it can't really hurt anything...

    Just loop until it finishes:

    void say(String w) {
      AudioPlayer p = audioClips.get(w);
      if (p == null) {
        println("Loading: " + w);
        p = minim.loadFile(w);
        audioClips.put(w, p);
      }
      p.rewind();
      p.play();
    
      //Loop...
      while(p.isPlaying()) {
        //Check back every 1/2 second
        Thread.sleep(500);
      }
    
      //When we break out of the loop, the player has completed...
      //...and we can move on
    }
    
Sign In or Register to comment.