Sound cuts off, then plays again

Hi,

I am sorry if I won’t solve the issue you are asking for but as a programmer, your code looks very repetitive :wink:

I often see Processing beginners that uses the framework to make programs that don’t use the full power of computing and programming in general that include functions, classes, loops, conditions…

In your case, we can clearly see a distinct pattern in your code that is :

if (millis() < 15000) {
    calm1.loop();
    calm2.pause();
    calm3.pause();
    calm4.pause();
    calm5.pause();
    calm6.pause();
    calm7.pause();
    calm8.pause();
    calm9.pause();
    calm10.pause();
    calm11.pause();
    calm12.pause();
    image(calm1, 0, 0, width, height);
}

Which is basically (in pseudocode) :

if (time < nth movie time) {
  pause all other movies except the nth one
  play nth movie
  display the nth movie
}

And this is the same process for calm / stress and trigger movies.

Also the way you are loading the movies is highly inefficient. You can improve that by first storing them into an array (for further access) and also by loading them in a loop.

Loops are essential in programming, you use them whenever you have to do something repeatedly or process a collection of data.

For example loading your movies :

// Create a list of movies
ArrayList<Movie> calmMovies = new ArrayList<Movie>();

for (int i = 0; i < 12; i++) {
  // Create the filename for that movie
  // i is going from 0 to 11 so add 1 to start with 1
  // Using + with strings is called concatenation
  String filename = "calm" + (i + 1) + ".mp4";
  
  // Load the actual movie
  movie = loadMovie(filename);

  // Add the created movie into the list
  calmMovies.add(movie);
}

Since Movie is a class, you can use an ArrayList to create an array of movies.

As you can see it’s reducing twelve instructions (that are the same) into a single 4 line loop! Pretty amazing :wink:

You can also create and add in the same loop the trigger and stress movies into their separate arraylist.

We can do the same for all the ifs conditions :

// We start with a time of 0 and then increase
// it by 15000 since you did it in your code
int time = 0;

// The time offset
int timeOffset = 15000;

// Loop through all the movies
for (int i = 0; i < 12; i++) {
  // If the timing is correct
  if (millis() < time + timeOffset) {
    // loop again for every movie
    for (int j = 0; j < 12; j++) {
      // Get the jth movie
      Movie calm = calmMovies.get(j);

      // if we are at the ith movie we play it 
      if (j == i) {
        calm.play();
      } else { // otherwise we pause it
        calm.pause();
      }
    }

    // Display the ith movie
    image(calmMovies.get(i), 0, 0, width, height);

    // We found the current movie so we break the loop
    break;
  }

  // We add the time offset
  time += timeOffset;
}

This is a little bit complicated but this is again the whole purpose of programming!
Now it’s even more reducing the number of lines in your program meaning more maintainability and bug free code.

You can check the for loop documentation from Processing to learn more about it.

Hope it helps!

Note : this topic is quite similar to the one I answered to on crossfading movies Crossfade multiple movies