movie.java
来自「This is processing for java examples.」· Java 代码 · 共 737 行 · 第 1/2 页
JAVA
737 行
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* Part of the Processing project - http://processing.org Copyright (c) 2004-07 Ben Fry and Casey Reas The previous version of this code was developed by Hernando Barragan This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/package processing.video;import processing.core.*;import java.io.*;import java.lang.reflect.*;import quicktime.*;import quicktime.io.QTFile;import quicktime.qd.*;import quicktime.std.*;import quicktime.std.movies.media.DataRef;import quicktime.util.QTHandle;import quicktime.util.RawEncodedImage;public class Movie extends PImage implements PConstants, Runnable { // no longer needing a reference to the parent because PImage has one //PApplet parent; Method movieEventMethod; String filename; Thread runner; PImage borderImage; boolean removeBorders = true; boolean play; boolean repeat; boolean available; int fps; /** * The QuickTime for Java "Movie" object, made public * in case anyone wants to play with it. */ public quicktime.std.movies.Movie movie; QDRect movieRect; QDGraphics movieGraphics; boolean firstFrame = true; RawEncodedImage raw; /* static { try { //System.out.println("jlp = " + System.getProperty("java.library.path")); QTSession.open(); } catch (QTException e) { e.printStackTrace(); } // shutting off for 0116, hoping for better exception handling QTRuntimeException.registerHandler(new QTRuntimeHandler() { public void exceptionOccurred(QTRuntimeException e, Object obj, String s, boolean flag) { System.err.println("Problem inside Movie"); e.printStackTrace(); } }); } */ public Movie(PApplet parent, String filename) { this(parent, filename, 30); } public Movie(final PApplet parent, final String filename, final int ifps) { // this creates a fake image so that the first time this // attempts to draw, something happens that's not an exception super(1, 1, RGB); // http://dev.processing.org/bugs/show_bug.cgi?id=882 //SwingUtilities.invokeLater(new Runnable() { //public void run() { init(parent, filename, ifps); //} //}); } public void init(PApplet parent, String filename, int fps) { this.parent = parent; this.fps = fps; try { QTSession.open(); } catch (QTException e) { e.printStackTrace(); return; } // first check to see if this can be read locally from a file. // otherwise, will have to load the file into memory, which is // gonna make people unhappy who are trying to play back 50 MB // quicktime movies with a locally installed piece exported // as an application. try { try { // first try a local file using the dataPath. usually this will // work ok, but sometimes the dataPath is inside a jar file, // which is less fun, so this will crap out. File file = new File(parent.dataPath(filename)); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); //init(parent, movie, ifps); //return; } } catch (Exception e) { } // ignored // read from a folder local to the current working dir // called "data". presumably this might be the data folder, // though that should be caught above, if such a folder exists. /* if (movie == null) { try { File file = new File("data", filename); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); init(parent, movie, ifps); return; } } catch (QTException e2) { } } */ // read from a file just hanging out in the local folder. // this might happen when the video library is used with some // other application, or the person enters a full path name if (movie == null) { try { File file = new File(filename); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); //init(parent, movie, ifps); //return; } } catch (QTException e1) { } } } catch (SecurityException se) { // online, whups. catch the security exception out here rather than // doing it three times (or whatever) for each of the cases above. } // if the movie can't be read from a local file, it has to be read // into a byte array and passed to qtjava. it's annoying that apple // doesn't have something in the api to read a movie from a friggin // InputStream, but oh well. it's their api. if (movie == null) { byte data[] = parent.loadBytes(filename); //int dot = filename.lastIndexOf("."); // grab the extension from the file, use mov if there is none //String extension = (dot == -1) ? "mov" : // filename.substring(dot + 1).toLowerCase(); try { movie = fromDataRef(new DataRef(new QTHandle(data))); } catch (QTException e) { e.printStackTrace(); } } /* URL url = null; this.filename = filename; // for error messages if (filename.startsWith("http://")) { try { url = new URL(filename); DataRef urlRef = new DataRef(url.toExternalForm()); movie = fromDataRef(urlRef); init(parent, movie, ifps); return; } catch (QTException qte) { qte.printStackTrace(); return; } catch (MalformedURLException e) { e.printStackTrace(); return; } } // updated for new loading style of 0096 ClassLoader cl = parent.getClass().getClassLoader(); url = cl.getResource("data/" + filename); if (url != null) { init(parent, url, ifps); return; } try { try { File file = new File(parent.dataPath(filename)); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); init(parent, movie, ifps); return; } } catch (Exception e) { } // ignored try { File file = new File("data", filename); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); init(parent, movie, ifps); return; } } catch (QTException e2) { } try { File file = new File(filename); if (file.exists()) { movie = fromDataRef(new DataRef(new QTFile(file))); init(parent, movie, ifps); return; } } catch (QTException e1) { } } catch (SecurityException se) { } // online, whups */ if (movie == null) { parent.die("Could not find movie file " + filename, null); } // we've got a valid movie! let's rock. try { // this is probably causing the 2 seconds of audio // disabled pre-preroll on 0126 because of security problems //movie.prePreroll(0, 1.0f); movie.preroll(0, 1.0f); // this has a possibility of running forever.. // should probably happen on the thread as well. while (movie.maxLoadedTimeInMovie() == 0) { movie.task(100); // 0106: tried adding sleep time so this doesn't spin out of control // works fine but doesn't really help anything //try { //Thread.sleep(5); //} catch (InterruptedException e) { } } movie.setRate(1); //fps = ifps; // register methods parent.registerDispose(this); try { movieEventMethod = parent.getClass().getMethod("movieEvent", new Class[] { Movie.class }); } catch (Exception e) { // no such method, or an error.. which is fine, just ignore } // and now, make the magic happen runner = new Thread(this); runner.start(); } catch (QTException qte) { qte.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* public Movie(PApplet parent, URL url) { init(parent, url, 30); } public Movie(PApplet parent, URL url, int ifps) { init(parent, url, ifps); } public void init(PApplet parent, URL url, int ifps) { String externalized = url.toExternalForm(); System.out.println("externalized is " + externalized); // qtjava likes file: urls to read file:/// not file:/ // so this changes them when appropriate if (externalized.startsWith("file:/") && !externalized.startsWith("file:///")) { externalized = "file:///" + url.getPath(); } // the url version is the only available that can take // an InputStream (indirectly) since it uses url syntax //DataRef urlRef = new DataRef(requestFile); try { System.out.println(url); System.out.println(externalized); DataRef urlRef = new DataRef(externalized); System.out.println(urlRef); movie = fromDataRef(urlRef); init(parent, movie, ifps); } catch (QTException e) { e.printStackTrace(); } } */ /** * Why does this function have to be so bizarre? i love the huge * constants! i think they're neato. i feel like i'm coding for * think pascal on my mac plus! those were happier times. */ private quicktime.std.movies.Movie fromDataRef(DataRef ref) throws QTException { return quicktime.std.movies.Movie.fromDataRef(ref, StdQTConstants4.newMovieAsyncOK | StdQTConstants.newMovieActive); } /* public void init(PApplet parent, quicktime.std.movies.Movie movie, int ifps) { this.parent = parent; try { // this is probably causing the 2 seconds of audio movie.prePreroll(0, 1.0f); movie.preroll(0, 1.0f); // this has a possibility of running forever.. // should probably happen on the thread as well. while (movie.maxLoadedTimeInMovie() == 0) { movie.task(100);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?