Play two movies with the microphone as trigger instead of mousepressed and released

i would like to play two movies - the first one when it is quiet (this one as loop) and the second one when the microphone sends a noise (the second video should play once through). when another noise is comming, the second video should restart again but only after it played through. my problem is, that it doesnt play through the whole length but only as long as the noise is repeated. i have no idea, how i could do this. can anybody help me?
here is my code

import processing.video.*;
import processing.sound.*;
Amplitude amp;
AudioIn in;

Movie myMovie;
Movie myMovie2;

void setup() {
  size(700, 500);
  
    //mikrofon aktivieren
  amp = new Amplitude(this);
  in = new AudioIn(this, 0);
  in.start();
  amp.input(in);
  
  myMovie = new Movie(this, "test_auge_1080_conv.mp4");
  myMovie2 = new Movie(this, "auge_verzerren_2_conv.mp4");
  myMovie.loop();
  myMovie2.loop();
  myMovie.pause();
  myMovie2.pause();

}

void draw() {
    
    if (amp.analyze() < 0.015){ //empfindlichkeit mikrofon
       image(myMovie, 0, 0,500,500);
        myMovie.play();
        myMovie2.pause();
                        
    } else if (amp.analyze() > 0.015){
        image(myMovie2, 200, 0,500,500);
          myMovie.pause();
          myMovie2.play();
      }
  println(amp.analyze());
   
}
void movieEvent(Movie m) {
  m.read();
}

You can use a variable to keep track which movie is playing. Below is just to give you the idea; your code will not run on my setup (not sure why, possibly missing libraries).

The variable is called movieNumber. It’s set based on the microphone level. The below should

  1. play movie 1 or movie 2 depending on sound level
  2. switch to movie 2 based on sound level if movie 1 is playing
  3. finish movie 2 and go back to (1)

As said, I can not test as it will not run. I hope that you understand the idea.

import processing.video.*;
import processing.sound.*;
Amplitude amp;
AudioIn in;

Movie myMovie;
Movie myMovie2;

int movieNumber = 0;

void setup()
{
  size(700, 500);

  //mikrofon aktivieren
  amp = new Amplitude(this);
  in = new AudioIn(this, 0);
  in.start();
  amp.input(in);

  myMovie = new Movie(this, "test_auge_1080_conv.mp4");
  myMovie2 = new Movie(this, "auge_verzerren_2_conv.mp4");
  myMovie.loop();
  myMovie2.loop();
  myMovie.pause();
  myMovie2.pause();
}

void draw()
{
  if (amp.analyze() < 0.015) //empfindlichkeit mikrofon
  {
    // do not change if movie 2 is playing
    if (movieNumber != 2)
    {
      movieNumber = 1;
    }
  } //
  else if (amp.analyze() > 0.015)
  {
    // switch to movie 2
    movieNumber = 2;
  }


  if (movieNumber == 1)
  {
    image(myMovie, 0, 0, 500, 500);
    myMovie.play();
    myMovie2.pause();
  } //
  else if (movieNumber == 2)
  {
    // if movie is not finished yet
    if (myMovie2.isAvailable())
    {
      image(myMovie2, 200, 0, 500, 500);
      myMovie.pause();
      myMovie2.play();
    } //
    else
    {
      //
      movieNumber = 0;
    }
  }

  println(amp.analyze());
}

void movieEvent(Movie m)
{
  m.read();
}

hi @sterretje
thanks for your help. unfortunately it doesn’t work. when i run it, only the first movie is running. the second one shows up but doesn’t play. when i start it a second time, the second movie doesn’t show at all anymore. only the first is running.
by the was - the command isAvailable() provocates a mistake. i took the command available() instead. but i don’t think, that this is the reason, why it doesn’t work at all.
i understand the basic idea, but it doesn’t seem to be much different to my simpler way.
when i think about the function at all, then i can compare it with the command mousePressed - this command only works as long as the mouse is pressed. so it seems, as if it is not possible to just use an impulse as a trigger. therefore is a command like mouseReleased or a key-command nessessary.
if i use a key, then it works - but a key-command does work different. unfortunately i have no idea, how i could write the command that it would work like a key-command…
or am i on the wrong way?
cheers

I’m sorry to hear that it does not wortk and I apologise for the mistake with available().

I should have told you that I’m new to Processing and don’t know anything about playing movies.

Can you show the code that does that? Maybe I can figure something out based on that.

I do not know what you mean by that.

the problem is, when one single clapp is the trigger to play the movie, it does play it only for a few milli-seconds and then stops. i would like that it plays the hole movie without having to clapp the hole time. here is a simple script which shows the problem.

import processing.video.*;
import processing.sound.*;
Amplitude amp;
AudioIn in;

Movie myMovie;

void setup() {
  size(700, 500);
  
  //mikrofon aktivieren
  amp = new Amplitude(this);
  in = new AudioIn(this, 0);
  in.start();
  amp.input(in);
  
  myMovie = new Movie(this, "test_auge_1080_conv.mp4");
  myMovie.loop();
  myMovie.pause();
}

void draw() {    
    if (amp.analyze() > 0.015){ //empfindlichkeit mikrofon
        image(myMovie, 0, 0,500,500);
        myMovie.play();     
    } 
  println(amp.analyze());
}

void movieEvent(Movie m) {
  m.read();
}

Figured out that I did not have the libraries installed. The below code will play a movie continuously; when a clap is detected, it will switch to another movie that plays for the duration of that movie. One second after that other movie is finished, it will switch back to the first movie and continue looping that.

I’m not 100% sure if it is exactly what you need but it should give you enough ammunition to work with. If you have questions, please ask. Note that I tested with mouse clicks, not with the clap detector.

import processing.video.*;
import processing.sound.*;

Amplitude amp;
AudioIn in;

// two movie objects
Movie myMovie;
String fileMovie1 = "C:\\Users\\Wim\\Videos\\2023-02-16 17-17-40.mp4";
Movie yourMovie;
String fileMovie2 = "C:\\Users\\Wim\\Videos\\VID-20230522-WA0027.mp4";
// movie that is currently playing
Movie currentMovie;

// last time that a frame was read
int lastReadTime = 0;
// timeout for play-once movie; 1 second
int readTimeout = 1000;

/*
 2023-02-16 17-12-43.mkv
 2023-02-16 17-17-40.mp4
 2023-02-16 17-21-54.mp4
 VID-20230522-WA0027.mp4
 */


void setup()
{
  size(800, 800);
  background(0);

  amp = new Amplitude(this);
  in = new AudioIn(this, 0);

  // first movie, will play continously
  myMovie = new Movie(this, fileMovie1);
  // second movie, plays once after clap
  yourMovie = new Movie(this, fileMovie2);

  // set the current movie to myMovie
  currentMovie = myMovie;
  // and loop it
  currentMovie.loop();
}

void draw()
{
  image(currentMovie, 0, 0);

  // switch to
  soundDetector();

  // if the current movie is yourMovie which plays only once
  if (currentMovie == yourMovie)
  {
    // check if we did not have data for a specified time
    if (millis() - lastReadTime >= readTimeout)
    {
      println("Switching back to 'myMovie'");
      // stop the current movie (yourMovie) so a new play will start it from the beginning
      currentMovie.stop();
      // switch to the looping movie (myMovie)
      currentMovie = myMovie;
      // and (continue) playing
      currentMovie.play();
    }
  }
}

void mouseClicked()
{
  if (currentMovie == myMovie)
  {
    println("Switching to 'yourMovie'");
    currentMovie.pause();
    currentMovie = yourMovie;
    currentMovie.play();
  }
}

/*
Sound dection; start other movie if a sounc is detected
*/
void soundDetector()
{
  // if a clap was detected
  if (amp.analyze() > 0.015)
  {
    // if we were playing the (looping) myMovie
    if (currentMovie == myMovie)
    {
      println("Switching to 'yourMovie'");
      // pause the looping movie (myMovie) so the next time that we play it, it starts where it paused
      currentMovie.pause();
      // change the movie to yourMovie
      currentMovie = yourMovie;
      // and play it
      currentMovie.play();
    }
  }
}

// Called every time a new frame is available to read
void movieEvent(Movie m)
{
  // keep track of the last time that we read a frame
  lastReadTime = millis();

  // read frame depending on movie that provided the frame
  if (m == myMovie)
  {
    myMovie.read();
  } //
  else if (m == yourMovie)
  {
    yourMovie.read();
  }
}

hi @sterretje
thank you for your script. first i thought with mouseClicked it is easy to make it work. my problem was the clapp detector. that is the one, that does not function like mouseClicked, mouseReleased. the mouseClicked is a combination of mousePressed and mouseReleased. that is, why it works with mouseClicked or also would with mouseReleased. but the sound detector works like mousePressed and that is very different to the others. if you look at the references it says:

Called once after a mouse button has been pressed and then released

Called once after every time a mouse button is pressed

Called every time a mouse button is released

after a long time of testing i saw that you didn’t start the microphone in your script, which is the reason, why it didn’t work - but after i corrected this - it does what it should - so cool !!! thank you so much !!!
here is the corrected script with starting the microphone as trigger

import processing.video.*;
import processing.sound.*;

Amplitude amp;
AudioIn in;

// two movie objects
Movie myMovie;
String fileMovie1 = "test_auge_1080_conv.mp4";
Movie yourMovie;
String fileMovie2 = "auge_verzerren_2_conv.mp4";
// movie that is currently playing
Movie currentMovie;

// last time that a frame was read
int lastReadTime = 0;
// timeout for play-once movie; 1 second
int readTimeout = 1000;


void setup()
{
  size(800, 800);
  background(0);

  amp = new Amplitude(this);
  in = new AudioIn(this, 0);
  //start the microphone
  in.start();
  amp.input(in);

  // first movie, will play continously
  myMovie = new Movie(this, fileMovie1);
  // second movie, plays once after clap
  yourMovie = new Movie(this, fileMovie2);

  // set the current movie to myMovie
  currentMovie = myMovie;
  // and loop it
  currentMovie.loop();
}

void draw()
{
  image(currentMovie, 0, 0);

  // switch to
  soundDetector();

  // if the current movie is yourMovie which plays only once
  if (currentMovie == yourMovie)
  {
    // check if we did not have data for a specified time
    if (millis() - lastReadTime >= readTimeout)
    {
      println("Switching back to 'myMovie'");
      // stop the current movie (yourMovie) so a new play will start it from the beginning
      currentMovie.stop();
      // switch to the looping movie (myMovie)
      currentMovie = myMovie;
      // and (continue) playing
      currentMovie.play();
    }
  }
}

void mouseClicked()
{
  if (currentMovie == myMovie)
  {
    println("Switching to 'yourMovie'");
    currentMovie.pause();
    currentMovie = yourMovie;
    currentMovie.play();
  }
}

/*
Sound dection; start other movie if a sounc is detected
*/
void soundDetector()
{
  // if a clap was detected
  if (amp.analyze() > 0.015)
  {
    // if we were playing the (looping) myMovie
    if (currentMovie == myMovie)
    {
      println("Switching to 'yourMovie'");
      // pause the looping movie (myMovie) so the next time that we play it, it starts where it paused
      currentMovie.pause();
      // change the movie to yourMovie
      currentMovie = yourMovie;
      // and play it
      currentMovie.play();
    }
  }
}

// Called every time a new frame is available to read
void movieEvent(Movie m)
{
  // keep track of the last time that we read a frame
  lastReadTime = millis();

  // read frame depending on movie that provided the frame
  if (m == myMovie)
  {
    myMovie.read();
  } //
  else if (m == yourMovie)
  {
    yourMovie.read();
  }
}

and you are certainly not a beginner!
now i am going to test, if i can combine this with 3 movies and even a livecam which starts at a certain point of the video - not yet any idea, how to do that ;o)
cheers