We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
at line 25 and a close } at line 35. That makes draw() do nothing until there's a movie to work on.
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:
Attention!
size() should always be the 1st line in draw
these lines must be within the else block in fileSelected