Change to video 2 if "key pressed".

Hello, so I have the code that follows next, what I pretend is to create a rule where if I press the key "2" it will play the video number 2 , but I don't know how to get it. I have all the videos in the same folder. The names are : video1.mp4 , video2.mp4...etc . I need a rule where I play video 2 by pressing key number 2. Please help me.

Code :

import processing.video.*;

Movie theMov; 
boolean isPlaying;
boolean isLooping;


void setup() { 
  size(600, 300);

  theMov = new Movie(this, "C:/Users/Filipe/Documents/Processing/libraries/video/video1.mp4");
  theMov.loop();  //plays the movie over and over
  isPlaying = true;
  isLooping = true;


}

void draw() { 
  background(0);
  image(theMov, mouseX-theMov.width/2, mouseY-theMov.height/4);
} 

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

void keyPressed() {
  if (key == 'p') {
    // toggle pausing
    if (isPlaying) {
      theMov.pause();
    } else {
      theMov.play();
    }
    isPlaying = !isPlaying;

  } else if (key == 'l') {
    // toggle looping
    if (isLooping) {
      theMov.noLoop();
    } else {
      theMov.loop();
    }
    isLooping = !isLooping;

  } else if (key == 's') {
    // stop playing
    theMov.stop();
    isPlaying = false;

  } else if (key == 'j') {
    // jump to a random time
    theMov.jump(random(theMov.duration()));

  }
}

Answers

  • You have enough sample code handy to be able to accomplish this yourself. I think what you are really missing is an understand of the process that should occur. So let's come up with a plan first.

    What should happen before the sketch even starts?

    • You should load the first video.

    • You should load the second video.

    • You should remember that the first video is the one that will be playing first.

    • You should start the first video playing.

    What happens when the sketch is redrawn?

    • You should draw a frame of the movie that is playing. You will know which movie to play because you have remembered which one is playing.

    What happens when a key is pressed?

    • If the first movie is playing and the key pressed was 2, then...

    • ... stop playing the first movie.

    • ... start playing the second movie.

    • ... and remember that second movie is now the one that is playing.

    I would suggest that you write a new sketch that does only the above. Use the existing sketch you have as reference - do NOT try to change it!

    Once you have attempted the above steps, post the code of your attempt here for more help.

  • import processing.video.*;
    
    Movie theMov; 
    int VideoPlaying;
    
    boolean Vid1 = false;
    boolean Vid2 = false;
    boolean Vid3 = false;
    boolean Vid4 = false;
    
    //Videos
    Movie Video1; 
    Movie Video2; 
    Movie Video3;
    Movie Video4;
    
    //Fim Videos
    
    void setup() { 
      size(600, 300);
    
      //videos
      Video1 = new Movie(this, "video1.mp4");
      Video2 = new Movie(this, "video2.mp4");
      Video3 = new Movie(this, "video3.mp4");
      Video4 = new Movie(this, "video4.mp4");
      //Fim videos
    
    }
    
    void draw() { 
    
      toogle();
      if (Vid1) {
    
        println("video1");
        Video2.stop();
        Video3.stop(); 
        Video4.stop();
        background(0);
        Video1.play();
        background(0);
        image(Video1, 0, 0, width, height);
      }
    
      if (Vid2) {
        println("video2");
        Video1.stop();
        Video3.stop(); 
        Video4.stop();
        background(0);
        Video2.play();
        image(Video2, 0, 0, width, height);
      }
    
      if (Vid3) {
        println("video3");
        Video1.stop();
        Video2.stop();
        Video4.stop();
        background(0);
        Video3.play();
        image(Video3, 0, 0, width, height);
      }
    
      if (Vid4) {
        println("video4");
        Video1.stop();
        Video2.stop();
        Video3.stop();
        background(0);
        Video4.play();
        image(Video4, 0, 0, width, height);
      }
    }
    void movieEvent(Movie m) { 
      m.read();
    } 
    
    void keyPressed() {
    
    
      //video
      if (key == '1') {
        VideoPlaying = 1 ;
        println(VideoPlaying);
      }
    
      if (key == '2') {
        VideoPlaying = 2 ;
        println(VideoPlaying);
      }
    
      if (key == '3') {
        VideoPlaying = 3 ;
        println(VideoPlaying);
      }
    
      if (key == '4') {
        VideoPlaying = 4 ;
        println(VideoPlaying);
      }
      //Fim Video
    
    }
    
    
    
    
    void toogle() {
    
      if (VideoPlaying == 1) {
        Vid1 = true;
        Vid2 = false;
        Vid3 = false;
        Vid4 = false;
      }
    
      if (VideoPlaying == 2) {
        Vid1 = false;
        Vid2 = true;
        Vid3 = false;
        Vid4 = false;
      }
    
      if (VideoPlaying == 3) {
        Vid1 = false;
        Vid2 = false;
        Vid3 = true;
        Vid4 = false;
      }
    
      if (VideoPlaying == 4) {
        Vid1 = false;
        Vid2 = false;
        Vid3 = false;
        Vid4 = true;
      }
    }
    

    ** WORKING :D**

Sign In or Register to comment.