How to read movie location in draw()

edited May 2015 in How To...

I am wondering how to read a movie file inside of draw()?

The example https://processing.org/reference/libraries/video/Movie.html reads the file inside of setup()

I want to be able to change what movie is being played while the sketch is running. I.e. use the up and down arrows to cycle through some videos in a folder. If the movie's location is assigned in setup() it cannot be changed once running.

For instance, if I try to use movie in the draw() it runs INCREDIBLY SLOW. As would be expected since it is creating a fresh movie object each frame.

import processing.video.*;
Movie myMovie;

File dir = new File("/Applications/SimplMix/ProcessingFork/vidz");
File[] fileArr = dir.listFiles();

void setup() {
  size(1280, 720);
}

void draw() {
  myMovie = new Movie(this, "/Applications/SimplMix/ProcessingFork/vidz/NewFall.mov");
  myMovie.loop();
  myMovie.play();
  image(myMovie, 0, 0);
}

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

Answers

  • Well this seams to work:

    import processing.video.*;
    
    Movie movie;
    
    void setup() {
      size(640, 360);
      background(0);
      // Load and play the video in a loop
      movie = new Movie(this, "transit.mov");
      movie.loop();
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void draw() {
      if (movie.available() == true) {
        movie.read(); 
      }
      image(movie, 0, 0, width, height);
    }
    
    void keyPressed(){
      if(key == 'n'){
      movie = null;
      movie = new Movie(this, "movie2.mov");
      movie.loop();
      }
    
    }
    

    But I would prefer to load all my videos at setup, in an array and just change the index, like:

    import processing.video.*;
    
    Movie[] movies = new Movie[2];
    int index = 0;
    
    void setup() {
      size(640, 360);
      background(0);
      // Load and play the video in a loop
      movies[0] = new Movie(this, "transit.mov");
      movies[1] = new Movie(this, "movie2.mov");
      movies[0].loop();
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void draw() {
      //  if (movie.available() == true) {
      //    movie.read(); 
      //  }
      image(movies[index], 0, 0, width, height);
    }
    
    void keyPressed() {
    
      if (key == 'n') {
        movies[index].stop();
        index++;
        index%=movies.length;
        movies[index].loop();
      }
    }
    
  • I like to even turn off draw() w/ noLoop(). And use redraw() inside movieEvent().
    Also I find background() & set() faster than image() for JAVA2D renderer:

    http://forum.processing.org/two/discussion/comment/40848/#Comment_40848

  • I look forward to exploring both these possibilities on the plane - thank you for the ideas.

    @_vk I like the idea in the second example. I am trying to have the option for a user to add more videos to the folder and then restart (in an ideal world no restart needed the array could be refreshed).

    Is there a way to have only one movie object that has a new file path loaded from this array? I think it may be possible. Once again, I'll have to hack around.

Sign In or Register to comment.