HELP! How to play different audio files in one sketch?

edited June 2017 in Library Questions

Hey guys, On one hand, I'm trying to play 10 different audio files in one sketch with minim. On the other hand, I need the program to ignore every following mouse press if the player is playing until its done. However, I dont get how to set up the "false" command. I would be so happy if someone could take a look at my sketch and help me out! Thanks a lot.

import ddf.minim.*;

Minim minim;
AudioPlayer[] player;

String[] SONGS = {"Fact01.mp3", "Fact02.mp3", "Fact04.mp3", "Fact05.mp3", "Fact06.mp3", "Fact07.mp3", "Fact08.mp3", "Fact09.mp3", "Fact10.mp3"};

void setup() {
  size(200, 200, P3D);
  minim = new Minim(this);
    player= new AudioPlayer[10];

  for (int i=0; i<SONGS.length; i++) {
    player[i] = minim.loadFile(SONGS[i]);
  }
}

void draw() {
  background(0);
}

void mousePressed() {

  if(player.isPlaying()==false){
  }

  int v=int(random(SONGS.length));
  player[v].rewind();
  player[v].play();

Answers

  • Answer ✓

    You need to notice player is an array, so player.isPlaying() does not apply to the array but to its members. I will do first:

    boolean isAnyPlaying(){
    
      boolean playing=false;
    
      for (int i=0; i<player.length; i++) {
         playing |= player[i].isPlaying()
      }
    
      return playing;
    }
    

    and now you need to introduce this previous code in your sketch:

    void mousePressed() {
    
      if(isAnyPlaying()==true){
         return;  // At least one mp3 is still playing
      }
    
      int v=int(random(SONGS.length));
      player[v].rewind();
      player[v].play();
    }
    

    However, this does not address your question of playing multiple files. I suggest to implement these changes and then comment if your code works as you want.

    Kf

  • @kfrajer

    Thank you sooo much for your help! Its working now as I want. :)

Sign In or Register to comment.