Input controlled video playback in Processing

Hi! I am reading capacitive sensors with Arduino and the number of the sensor touched is communicated to Processing, at the moment it is just 1 or 2. In Processing, I am trying to play a video depending on the number of the sensor received. I need to be able to switch between the different videos during the time of playback, after it is finished, and if the same number is pressed then the video should jump back to the start.

At the moment, I can switch between the videos but only from video1 to video2, when going back from video2 to video1 I get the audio of the movie1 but it keeps displaying the last frame of the video2. I would appreciate any insight into why is this happening and how could I fix it?
This is the code in Processing


    import processing.serial.*;
    import processing.video.*;
    
    Movie video1, video2;
    
    Serial port;
    char in;
    char previousIn;
    boolean playing = false;
    //float time = 0;
    
    void setup() {  
      fullScreen(JAVA2D);
      frameRate(25);
      video1 = new Movie(this, "redFIN.mp4");
      video2 = new Movie(this, "greenFIN.mp4");
      port = new Serial(this, Serial.list()[0], 9600);
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void draw() {  
      if ( port.available() > 0) {  // If data is available,
        in = char(port.read());
        print(in);
      }
    
      if (in == '1') {  
        video1.play();
        video2.stop();
        in = previousIn;
        if (in == previousIn) {
          video1.jump(0);
        }
      }
      image(video1, 0, 0, width, height);
    
    
      if (in =='2') {
        video2.play();
        video1.stop();
        in = previousIn;
        if (in == previousIn) {
          video2.jump(0);
        }
      }
      image(video2, 0, 0, width, height);
    }

Hello!
This is because you have both image(video1, 0, 0, width, height) and image(video2, 0, 0, width, height) in the main draw loop. Since video2 is always the last to be drawn, its frame will be drawn over video1.

You can fix this easily by setting boolean variables for video1 and video2.

//global
boolean vid1Playing = false;
boolean vid2Playing = false; 
// on trigger
if( in =='2'){
    video2Playing = true;
    video1Playing = false;
}
// main draw loop
   if(video1Playing){
        image(video1playing,0,0,width,height);
   }
   if(video2Playing){
        image(video2playing,0,0,width,height);
   }

This will make it so that only video2 is drawn when it is playing, etc.

From experience, videos that have stopped will still trigger the movieEvent callback and feed its last frame.