How do I select a movie using selectInput() ?

edited August 2015 in Library Questions

Here's my code for reference:

import processing.video.*;
Movie myMovie;
File selection;
int x = 0;
int i = 0;
void setup() {
 selectInput("Select a file to process:", "fileSelected", selection); 
 size(640, 480);


}

void fileSelected(File selection)
{
if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
 myMovie = new Movie(this, selection.getAbsolutePath());
 myMovie.loop();
}

void draw() {

 image(myMovie, 0,0,width,height);
 myMovie.noLoop();

 float y = myMovie.time();

 println("time = " + myMovie.time());
 println("duration = " + myMovie.duration());
 if(y > myMovie.duration() - 0.1)
 exit();

}

// Called every time a new frame is available to read
void movieEvent(Movie m) {
 m.read();
}

I suppose my code is self-explanatory.

What I am trying to do is open a file browser, select a movie I want to load and then play it in a processing window.

However when I run this code, I get a null pointer exception at

'image(myMovie, 0,0,width,height);'

I am not able to understand the right structure of code which will let me achieve what I want to.

Please suggest me right format to structure my code.

EDIT:

I changed the command to load movie to:

myMovie = new Movie("selection.getAbsolutePath()");

now I get an error The constructor movie(string) is undefined

Answers

  • The problem is that the system doesn't pause for selectInput() to return a File, but it immediately starts draw() while there's still no movie to process. Try inserting

     if (myMovie != null) {
    

    at line 25 and a close } at line 35. That makes draw() do nothing until there's a movie to work on.

  • edited August 2015

    You can place a delay() loop at the end of setup() in order to halt execution of draw() until movieEvent() gets its 1st read(), and thus width got some dimension:

    while (myMovie == null || myMovie.width == 0)  delay(100);
    
  • Answer ✓
    // demonstrate loading of a video via selectInput
    
    import processing.video.*;
    
    Movie myMovie = null;
    File selection;
    
    int x = 0;
    int i = 0;
    
    void setup() {
      // size should always be the 1st line in draw(): init
      size(640, 480); 
      // set default path for fileSelected
      selection = new File(dataPath("") + "//*.mov");
      // start callback function 
      selectInput("Select a file to process:", "fileSelected", selection);
    }
    
    void draw() {
      // check whether the movie is already loaded
      if (myMovie != null && myMovie.width > 0) {
        // load success 
        image(myMovie, 0, 0, width, height);
        // myMovie.noLoop();
    
        float y = myMovie.time();
    
        println("time = " + myMovie.time());
        println("duration = " + myMovie.duration());
        if (y > myMovie.duration() - 0.1) {
          exit(); // QUIT
        } //  if
        //
      } else {
        // wait for callBack Function fileSelected
      } // else
    } // func
    
    // --------------------------------------------------
    
    void fileSelected(File selection)
    {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
        exit();  // QUIT
      } else {
        println("User selected " + selection.getAbsolutePath());
        println("Loading.......");
        text ("Loading.......", width/2, height/2);
        myMovie = new Movie(this, selection.getAbsolutePath());
        myMovie.loop();
        // myMovie.play();
      }
    }
    
    // Called every time a new frame is available to read
    void movieEvent(Movie m) {
      m.read();
    }
    // END
    
  • Answer ✓

    Attention!

    size() should always be the 1st line in draw

    these lines must be within the else block in fileSelected

     myMovie = new Movie(this, selection.getAbsolutePath());
     myMovie.loop();
    
Sign In or Register to comment.