在processing開發(fā)中需要長時間播放視頻時期奔,經(jīng)常會出現(xiàn)NullPointerException錯誤侧馅,可以使用PImage獲取Movie中的每一幀圖像的方式來替代直接在draw中直接使用Movie播放視頻,代碼如下:
import processing.video.*;
Movie movie;
PImage movieImage;
void setup() {
? size(800, 800);
? background(0);
? //加載及循環(huán)播放視頻
? movie = new Movie(this, "bottom.mp4");
? movie.loop();
}
// 不使用以下方法
//void movieEvent(Movie m) {
//? m.read();
//}
void draw() {
? // I use the synchronization here to avoid multiple calls to draw
? // before it finishes
? synchronized(this) {?
? ? if (movie.available()) {
? ? ? //loads a new frame
? ? ? movie.read();
? ? ? if (movieImage==null) {
? ? ? ? // delayed initialization, because until the first frame is read
? ? ? ? // the movie object does not now its size
? ? ? ? movieImage=createImage(movie.width, movie.height, RGB);
? ? ? ? movieImage.loadPixels();
? ? ? }
? ? ? movie.loadPixels();
? ? ? //copies pixels to a PImage
? ? ? System.arraycopy(movie.pixels, 0, movieImage.pixels, 0, movie.pixels.length);
? ? ? movieImage.updatePixels();
? ? }
? ? if (movieImage!=null) {
? ? ? //draw the image/video
? ? ? image(movieImage, 0, 0, width, height);
? ? }
? }
}