Playing different videos from Serial

I’m very new to Processing, so please forgive me if I’m missing something very basic!
I’m trying to use an Arduino/Serial input that will trigger different videos to play in a Processing sketch. The code below is based off of this one that I found in an old Arduino forum, for a single video: Trigger a video file from computer to play when someone approaches the sonar. - #16 by skypickle - Project Guidance - Arduino Forum

I’ve tried to do it with 2 videos instead. This works on first try - when I press button 1, video 1 plays, when I stop pressing it, video 1 pauses. Same thing when I press button 2. However, I can only go from video 1 to video 2. After video 2 plays, I can’t go back to video 1 when I press button 1.
Another issue I’m encountering is that I’m trying to have the screen clear to a blank page when no buttons are pressed (serial input = 0), but this part doesn’t seem to be working.

Any help would be greatly appreciated! Thank you in advance!

import processing.serial.*;
import processing.video.*;

Serial myPort;  // Create object from Serial class
int val = 0;     // Data received from the serial port
String PATH1 = "Clip1.mp4";
String PATH2 = "Clip2.mp4";
Movie mov1;
Movie mov2;
String inData = "";
boolean playing1 = false;
boolean playing2 = false;

void setup() {
  size(1920, 816);
  frameRate(23.98);
  mov1 = new Movie(this, PATH1);
  mov1.speed(1);
  mov1.volume(0);
  mov1.pause();
  
  mov2 = new Movie(this, PATH2);
  mov2.speed(1);
  mov2.volume(0);
  mov2.pause();

  String COM1 = Serial.list()[1]; 
  myPort = new Serial(this, COM1, 9600);
  
}

void movieEvent(Movie m) {
  if (m == mov1) {
    mov1.read();
  } else if (m == mov2) {
    mov2.read();
  }
}

void draw() {
  
  if ( myPort.available() > 0) {  
         try { 
            inData = myPort.readStringUntil('\n');
            if ( inData == null ) {
               throw new Exception("Error: " + inData);
            }
            else
            inData = inData.trim();
            val = Integer.parseInt(inData);
            println(val);
            if (val == 1) { //If button 1 is pressed, mov 1 is played
              if (!playing1) {
                mov1.jump(0);
                mov1.loop();
                playing1 = true;
              }
            } else if (val == 2) { //If button 2 is pressed, mov 2 is played
              if (!playing2) {
                mov2.jump(0);
                mov2.loop();
                playing2 = true;
              }
            } else if (val == 0) {
               background(255); 
               mov1.pause();
               mov2.pause();
               playing2 = false;
               playing1 = false;
            }
            
       } catch (Exception ex) {
         println("Exception:" + ex.toString() + " Data:" + inData);
       }
       println(">" + inData+" -> "+val+"<");
  } 
  

  image(mov1, 0, 0, width, height);
  image(mov2, 0, 0, width, height);
}
1 Like

Hi there,
A have a few pointers I hope can help.

mov1.loop() and mov2.loop() should be placed in setup() instead of within the draw() loop.
Once this is done void movieEvent(Movie m) will read each video frame and loop will remain constant.

The background() function allows you to set a solid colour to the whole sketch.
see: Reference / Processing.org

I think the reason you unable to go back to seeing video 1 is that you are always drawing it first.

Consider changing the last two lines to something like:

if (val == 1 && playing1 == true) {
    image(mov1, 0, 0, width, height);
}
else if (val == 2 && playing2 == true) {    //
    image(mov2, 0, 0, width, height);
}
else {
    background(0);    // This fills the window with black. You need to run background() outside of the conditions because  mov.pause() will still display the video paused image frame in image(). 
}

or,

I think you could just place each image(mov [1 or 2], 0,0, etc…) function directly in your conditional statements.

Hope this helps. Best of luck.