Ruta
Ruta

Reputation: 13

Playing video in Processing

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 the video should jump back to the start.

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);
    }

At the moment, I can switch between the videos but only from movie1 to movie2, when going back from movie2 to movie1 I get the audio of the movie1 but it keeps displaying the last frame of the movie2.

I would appreciate any insight into why is this happening.

Upvotes: 1

Views: 382

Answers (1)

George Profenza
George Profenza

Reputation: 51867

You're super close, but still have a couple of areas that won't behave the you expect:

  1. You draw both video1 and video2 using image(): you'll want to only render a single video
  2. the if (in == previousIn) conditions should probably happen before setting previousIn (which btw you're setting the ohter way around). (otherwise the condition will always be true)

Here's a version using a third Movie variable which is simply a reference to either video1 or video2 depending on context:

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

Movie video1, video2, currentVideo;

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");
  // default to video1
  currentVideo = video1;
  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();
    if (in == previousIn) {
      video1.jump(0);
    }
    previousIn = in;
    currentVideo = video1;
  }

  if (in =='2') {
    video2.play();
    video1.stop();
    if (in == previousIn) {
      video2.jump(0);
    }
    previousIn = in;
    currentVideo = video2;
  }
  
  image(currentVideo, 0, 0, width, height);
}

Upvotes: 1

Related Questions