Help needed getting Processing to open video files.

hello,

I am wanting to replace the following code. I have a capacitive touch sensor attached to processing that opens up a random image every time the sensor is held, when the sensor is let go this image disappear.

I want to essentially replace the image files with 26 short video clips at 25 frame rate. Im not sure how to change this code to make that happen.

Any help would be great!

import processing.serial.*;

int threshold = 30; 
PImage [] picArray = new PImage [26]; 
Serial myPort;

boolean holdImage=false;
int imgIndex;

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

  for (int i=0; i<picArray.length; i++)
    picArray[i]=loadImage(i + ".png");

  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
}


void draw() {

  background (255, 255, 255); 
  if (holdImage==true) { 
    image(picArray[imgIndex], 0,0);
  }
}


void serialEvent(Serial myPort) {

  String inString = myPort.readStringUntil('\n');

  if (inString != null) {

    inString = trim (inString);
    float[] touches = float (split(inString, ","));

    if (touches.length >=1) {
      int touch1Value = touches[0] >= threshold ? 1: 0;

      if (touch1Value == 1) {
        if (holdImage == false) {
          imgIndex=int(random(picArray.length));
          holdImage=true;
        }
      } else
        holdImage=false;  //RELEASE holder so to detect next usr press event
    }
  }
}

Answers

  • Have you tried simply playing a movie?

  • Unfortunately I'm not very skilled at using processing. I wouldn't know how to play a movie.Yet I want this movie to be started as soon as the capacitive sensor is held down, and stop returning to a white screen when It is finished.

    Any examples would be great, I'll have 26 video labelled 123..etc

  • In Processing, install the library called video using the library manager. After that, you can open one of the examples under File >> examples and go to libraries >> video >> movie and run some of the examples there. After you have an example running, then you need to use that example to play a couple of your movies. This is what I would do first anyways...

    Kf

  • I've still not managed to resolve this guys :(

    I literary just want the code to play a random array of videos instead of images when the sensor is held

  • I wouldn't know how to play a movie

    Here is the example of how to play a movie:

    Here is the example for using play():

    I want to essentially replace the image files with 26 short video clips

    One approach is to create an array of movies instead of images. So, instead of:

    PImage [] picArray = new PImage [26]; 
    

    ...create an array of Movies:

    Movie [] movArray = new Movie [26];
    
  • Try this

    https://forum.processing.org/two/discussion/4333/how-can-i-get-multiple-videos-to-play-one-after-another-and-triggered-by-specific-keys

    It is important you play the basic movies first. You need to figure out where to place your movie files, to become familiar with Processing and this is an important step so to be able to use it with your capacitive sensor project.

    Kf

  • edited April 2017

    I have attempted at the below code.

    The code does play a video from the capacitive touch however it doesn't appear to be randomised.

    Also when the sensor is released the video pauses instead of disappearing the sound also carries on for some reason.

    import processing.video.*;
    import processing.serial.*;
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;
    
    //PImage [] picArray = new PImage [26];
    
    Serial myPort;
    
    boolean holdImage=false;
    
    int imgIndex;
    
    void setup(){
    
      size (500,500);
    
      myMovie = new Movie[3];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
    
      myMovie[0].pause();
      myMovie[1].pause();
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw(){
    
      background (255, 255, 255); 
    
      if (holdImage){ 
        myMovie[index].read();
      }  
      image(myMovie[index], 0, 0);  
    
    }
    
    void serialEvent(Serial myPort){
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null){
    
        inString = trim (inString);
        float[] touches = float (split(inString, ","));
    
        if (touches.length >=1) {
          int touch1Value = touches[0] >= threshold ? 1 : 0;
    
          if (touch1Value == 1) {
            if (holdImage == false) {
              myMovie[1].play();
              index = 1;
              holdImage=true;
            }
          } else {
            holdImage=false;
          }
        }
      }
    }
    
  • The code does play a video from the capacitive touch however it doesn't appear to be randomised.

    lines 60, 61 have a hard-coded value in there. is that what you want?

  • No, I basically want a randomised video to be displayed when the sensor is held.

    Once the sensor is released I want the video to stop playing and clear from the screen.

  • then you need to randomise the index values in lines 60 and 61. if it says "1" then you'll only every see video 1.

  • I'm kinda new at processing, how would I do this for a video?

  • https://forum.processing.org/two/discussion/comment/85389/#Comment_85389

    Also you use this:

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

    Also remove holdimage in draw as it is not needed. If you want to hold your image, you simply pause all your movies.

    Now for the random part:

           if (touch1Value == 1) {
             for(Movie mm:myMovie)
               mm.pause();
               index = (int) random(2);  //0 or 1 in this case
               myMovie[index].play();
              }
            }      
    

    Kf

  • After making these alterations it now says - error, disabling serial event()

    My code is below.

    import processing.video.*;
    import processing.serial.*;
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;
    
    //PImage [] picArray = new PImage [26];
    
    Serial myPort;
    
    //boolean holdImage=false;
    
    int imgIndex;
    
    void setup(){
    
      size (500,500);
    
      myMovie = new Movie[3];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
    
      myMovie[0].pause();
      myMovie[1].pause();
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw(){
    
      background (255, 255, 255); 
    
      //if (holdImage){ 
        myMovie[index].read();
      }  
      //image(myMovie[index], 0, 0);  
    
    
    void movieEvent(Movie m){
      m.read();
    }
    
    void serialEvent(Serial myPort){
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null){
    
        inString = trim (inString);
        float[] touches = float (split(inString, ","));
    
        if (touches.length >=1) {
          int touch1Value = touches[0] >= threshold ? 1 : 0;
    
          if (touch1Value == 1) {
             for(Movie mm:myMovie)
               mm.pause();
               index = (int) random(2);  //0 or 1 in this case
               myMovie[index].play();
          }
    
        }
      }
    }
    
  • Line 23, it should be 2, not 3.

    Also, I will suggest running a code only for movies. When you know it is running, then you merge it with your serial code. Otherwise is more difficult to debug.

    Kf

  • The video file does not appear to be showing, It also does not recognise when I let go of the sensor

  • Try this.

    Kf

    import processing.video.*;
    import processing.serial.*;
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;    
    
    Serial myPort;
    int imgIndex;
    
    void setup() {    
      size (500, 500);
    
      myMovie = new Movie[2];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() { 
      background (255, 255, 255);  
      if (myMovie[index]!=null)
        image(myMovie[index], 0, 0);
    }  
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null) {
    
        inString = trim (inString);
        float[] touches = float (split(inString, ","));
    
        if (touches.length >=1) {
          int touch1Value = touches[0] >= threshold ? 1 : 0;
    
          if (touch1Value == 1) {
            for (Movie mm : myMovie)
              mm.pause();
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
          }
        }
      }
    }
    
  • When the sensor is held within this code both movies play at the same time, I just want one movies to play each time the sensor is held until it is let go.

    I feel this code is going in the right direction

  • Ok, so you need to use a variable that lock the state of your movie. It should choose a new movie only after the Arduino input goes below threshold.

    Kf

    boolean  lockMovie=false;
    
    void serialEvent(Serial myPort) {
     
      String inString = myPort.readStringUntil('\n');
     
      if (inString != null) {
     
        inString = trim (inString);
        float[] touches = float (split(inString, ","));
     
        if (touches.length >=1) {
          int touch1Value = touches[0] >= threshold ? 1 : 0;
     
          if (touch1Value == 1 && lockMovie==false) {
            for (Movie mm : myMovie)
              mm.pause();
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
            lockMovie=true;
          }
    
          if (touch1Value == 0){
              lockMovie=false;
          }
    
        }
      }
    }
    
  • That seems to work :)

    However when the sensor is released the randomised movie still plays for some reason?

    When the sensor is realised I want it to return to a blank/white screen.

  • Rewritten....

    Kf

    boolean  lockMovie=false;
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString == null) 
        return;
    
    
      inString = trim (inString);
      float[] touches = float (split(inString, ","));
    
      if (touches.length > 0)  {  
    
        if (touches[0] >= threshold) {
    
          if (lockMovie==false) {
    
            for (Movie mm : myMovie)
              mm.pause();
    
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
            lockMovie=true;
          }
        } else 
        lockMovie=false;
      }
    
    }
    
  • Copy lines 20 and 21 inside else block in line 27.

    Also modify draw like this:

     if (myMovie[index]!=null && lockMovie==true)
        image(myMovie[index], 0, 0);
    

    Kf

  • With this suggestion the movies appear and disappear when they should in regards to the sensor, however they don't seem to be playing it is only a still image of the first part of the movie.

    import processing.video.*;
    import processing.serial.*;
    
    boolean  lockMovie=false; 
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;    
    
    Serial myPort;
    int imgIndex;
    
    void setup() {    
      size (500, 500);
    
      myMovie = new Movie[2];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() { 
      background (255, 255, 255);  
       if (myMovie[index]!=null && lockMovie==true)
        image(myMovie[index], 0, 0);
    }  
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString == null) 
        return;
    
    
      inString = trim (inString);
      float[] touches = float (split(inString, ","));
    
      if (touches.length > 0)  {  
    
        if (touches[0] >= threshold) {
    
          if (lockMovie==false) {
    
            for (Movie mm : myMovie)
              mm.pause();
    
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
            lockMovie=true;
          }
        } else
        lockMovie=false;
        for (Movie mm : myMovie)
              mm.pause();
      }
    
    }
    
  • It is hard when I am not able to reproduce the problem. This is the reason I suggested working with playing only the movies. You could use the mouse event keypressed/keyreleased to simulate when the Arduino is pressed down or not.

    Kf

    import processing.video.*;
    //import processing.serial.*;
    
    boolean  lockMovie=false; 
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;    
    
    //Serial myPort;
    int imgIndex;
    
    void setup() {    
      size (500, 500);
    
      myMovie = new Movie[2];
    
      myMovie[0]  = new Movie(this, "transit.mov");
      myMovie[1]  = new Movie(this, "shortrun.mp4");
    
      myMovie[0].play();
      myMovie[0].pause();
      myMovie[1].play();
      myMovie[1].pause();
    
      //myPort = new Serial(this, Serial.list()[1], 9600);
      //myPort.bufferUntil('\n');
    }
    
    void draw() { 
      background (255, 155, 0);  
      if (myMovie[index]!=null && lockMovie==true)
        image(myMovie[index], 0, 0);
    }  
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    
    
    
    void keyPressed() {
    
      modified____serialEvent("101,00");
    }
    
    void keyReleased() {
    
      modified____serialEvent("0,00");
    }
    
    
    
    
    void modified____serialEvent(String inString) {
    
      //String inString = myPort.readStringUntil('\n');
    
      if (inString == null) 
        return;
    
    
      inString = trim (inString);
      float[] touches = float (split(inString, ","));
    
      if (touches.length > 0) {  
    
        if (touches[0] >= threshold) {
    
          if (lockMovie==false) {
    
            for (Movie mm : myMovie)
              mm.pause();
    
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
    
            println("..............Playing "+index);
            lockMovie=true;
          }
        } else {
          lockMovie=false;
          for (Movie mm : myMovie)
            mm.pause();
        }
      }
    
      println("Status lockMovie "+lockMovie);
      println("Status index "+index);
      println("Status inString "+inString);
    }
    
  • Answer ✓

    Line 62... You need a extra set of brackets to capture the block of that else. I made a mistake and removed those brackets before. That should solve the problem.

    Kf

  • That has solved the problem,

    However I want my movie to reset rather than pause and replay when the sensor is let go and then pressed.

    If this is figured out I think I will be done.

  • I managed to figure this out.

    Thank you very much for your help!

    My final code is below...

    import processing.video.*;
    import processing.serial.*;
    
    boolean  lockMovie=false; 
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;    
    
    Serial myPort;
    int imgIndex;
    
    void setup() {    
      size (500, 500);
    
      myMovie = new Movie[2];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() { 
      background (255, 255, 255);  
       if (myMovie[index]!=null && lockMovie==true)
        image(myMovie[index], 0, 0);
    }  
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString == null) 
        return;
    
    
      inString = trim (inString);
      float[] touches = float (split(inString, ","));
    
      if (touches.length > 0)  {  
    
        if (touches[0] >= threshold) {
    
          if (lockMovie==false) {
    
            for (Movie mm : myMovie)
              mm.pause();
    
            index = (int) random(2);  //0 or 1 in this case
            myMovie[index].play();
            lockMovie=true;
          }
        } else {
        lockMovie=false;
        for (Movie mm : myMovie)
              mm.stop();
      }
      }
    }
    
  • Perfect!

    Kf

  • I've tried to add 26 short 10 second movies to the code, however I can't seem to see the visuals I can only here the music playing.

    I just want my movies to be centred on the page so it is viewable, these movies are set at 500pixels - 500pixels.

    My code is below.

    import processing.video.*;
    import processing.serial.*;
    
    boolean  lockMovie=false; 
    
    Movie myMovie[];
    float t0;
    float t;
    int index = 0; 
    
    int threshold = 30;    
    
    Serial myPort;
    int imgIndex;
    
    void setup() {    
      size (500, 500);
    
      myMovie = new Movie[26];
    
      myMovie[0]  = new Movie(this, "1.mov");
      myMovie[1]  = new Movie(this, "2.mov");
      myMovie[2]  = new Movie(this, "3.mov");
      myMovie[3]  = new Movie(this, "4.mov");
      myMovie[4]  = new Movie(this, "5.mov");
      myMovie[5]  = new Movie(this, "6.mov");
      myMovie[6]  = new Movie(this, "7.mov");
      myMovie[7]  = new Movie(this, "8.mov");
      myMovie[8]  = new Movie(this, "9.mov");
      myMovie[9]  = new Movie(this, "10.mov");
      myMovie[10]  = new Movie(this, "11.mov");
      myMovie[11]  = new Movie(this, "12.mov");
      myMovie[12]  = new Movie(this, "13.mov");
      myMovie[13]  = new Movie(this, "14.mov");
      myMovie[14]  = new Movie(this, "15.mov");
      myMovie[15]  = new Movie(this, "16.mov");
      myMovie[16]  = new Movie(this, "17.mov");
      myMovie[17]  = new Movie(this, "18.mov");
      myMovie[18]  = new Movie(this, "19.mov");
      myMovie[19]  = new Movie(this, "20.mov");
      myMovie[20]  = new Movie(this, "21.mov");
      myMovie[21]  = new Movie(this, "22.mov");
      myMovie[22]  = new Movie(this, "23.mov");
      myMovie[23]  = new Movie(this, "24.mov");
      myMovie[24]  = new Movie(this, "25.mov");
      myMovie[25]  = new Movie(this, "26.mov");
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() { 
      background (255, 255, 255);  
       if (myMovie[index]!=null && lockMovie==true)
        image(myMovie[index], 0, 0);
    }  
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString == null) 
        return;
    
    
      inString = trim (inString);
      float[] touches = float (split(inString, ","));
    
      if (touches.length > 0)  {  
    
        if (touches[0] >= threshold) {
    
          if (lockMovie==false) {
    
            for (Movie mm : myMovie)
              mm.pause();
    
            index = (int) random(26);  //0 or 1 in this case
            myMovie[index].play();
            lockMovie=true;
          }
        } else {
        lockMovie=false;
        for (Movie mm : myMovie)
              mm.stop();
      }
      }
    }
    
  • If you run 3 movies for example, you have the same problem?

    Other comments:

    Don't use 26 in your code. Instead use for example:

    final int nMovies=26;

    To center the movie, use imageMode(CENTER); and replace line 55 with

    image(myMovie[index], width/2,height/2);

    Also change 79 and 80 with myMovie[index].pause();

    Lastly, change 88 and 89 with myMovie[index].stop();

    Kf

Sign In or Register to comment.