Playing Multiple sounds at different times

Hello everyone, my first post here. I’ve been wrestling with this code for a while now, I have managed to get it working to some degree, but I’ve run into an issue I can’t get my head around.

I want load sounds into an array. Then at set times play a random mp3 from the array, currently at 5, 6 and 7 second intervals.

The issue is the sounds play fine and at the right times first time through the code, then it starts to become overlapped like the millis(count) has caught up with itself and is no longer playing and counting in the order I want.

I’m looking for a way to maybe reset the count / millis back to 0 and run through the program again so it only plays sounds at the requested intervals.

Here is the code below. Thank you in advance for any help on the matter.
(understandably there are libraries and mp3 files not included my apologies for that)

//variables for millis //
int lasttimecheck;
int lasttimenew;
int lasttimenow;

// variables for amount of time. 3000 = 3 seconds for example // 
int timecheck;
int timenew;
int timenow;

import processing.sound.*;

AudioDevice device;
SoundFile[] file;

void setup() {
  size(640, 360);
  lasttimecheck = millis();
  lasttimenew = millis();
  lasttimenow = millis();

  // timings//

  timecheck = 5000; // 5 seonds 
  timenew = 6000; //  6 seonds 
  timenow = 7000; //  7 seconds 

  //number of items in the array 
  file = new SoundFile[24];

  //loop through all items in the array and look for file types // 
  for (int i = 0; i < file.length; i++) {
    file[i] = new SoundFile(this, (i) + ".aiff");

// first array list 5 seconds //
void timechange() {

  if (millis() > lasttimecheck + timecheck) {
    lasttimecheck = millis();
    file[int(random(0, 7))].play();

// second array list 6 sceonds // 
void timeextra() {

  if (millis() > lasttimenew + timenew) {
    lasttimenew = millis();
    file[int(random(8, 15))].play();
// second array list 7 seconds // 
void timedone() {

  if (millis() > lasttimenow + timenow) {
    lasttimenow = millis();
    file[int(random(16, 23))].play();

// play sounds // 

void draw() {


1 Like

Hi kristianjones81, I think the issue is caused by playing too faster code; in your functions you have to check if last play is yet playing …
(Keep random number generated)
the library offer this check method:

if (file.isPlaying()) {

Keep up to date…

1 Like

Obviously you have to test entire Array set:

boolean testPlay() {
  for (int i = 0; i < file.length; i++) {
      if (file[i].isPlaying()) {
          return = true;
      } // end if
    } // end for
  return false;
} // end testPlay

Sorry for my rough code but I writed it with a phone…

1 Like

also i think you should be aware of the scheduler you build:

// -1- timercheck only
// -2- random song number

//variables for millis //
int lastT1, lastT2, lastT3, timeT1=5000, timeT2=6000, timeT3=7000;

void setup() {
  lastT1 = lastT2 =lastT3 = millis();

void timerT1() {
  if (millis() > lastT1 + timeT1) {
    lastT1 = millis();
    println(nf(millis()/1000.0,1,1)+" timerT1 start song: "+int(random(0, 8)));

void timerT2() {
  if (millis() > lastT2 + timeT2) {
    lastT2 = millis();
    println(nf(millis()/1000.0,1,1)+" timerT2 start song: "+int(random(8, 16)));

void timerT3() {
  if (millis() > lastT3 + timeT3) {
    lastT3 = millis();
    println(nf(millis()/1000.0,1,1)+" timerT3 start song: "+int(random(16, 24)));

void draw() {

but if you want exactly that,
but hit a limit of the sound library,
pls. take a look at the Minim lib.
what might be more powerful,
still play many sound files at the same time results
not only into NOISE ( by concept )
it will also end up into playing issues what sometimes create even more bad noise.
( anyhow with that sound part i will not follow you )

1 Like

Thank you both for the most helpful replies. I will test out both methods and let you know.

Thank you once again.