- Installing QuickTime for Java
- Learning by Example
- Playing the Movie
- Wrapping Up
- References
Learning by Example
The best way to learn about the QuickTime for Java API is to work with some sample code. For this purpose, we've created a simple no-frills movie viewer that we affectionately call QTJViewer (source).
Loading a Movie
Our viewer uses a JFileChooser to allow the user to browse to his or her filesystem and point to a QuickTime-supported multimedia file that he or she wants to play. We store the absolute path to the filename in a String variable named movieName.
Next, we pass the value of movieName to our own getMovieScreen screen method. This method does the job of loading the movie. Our ultimate motivation is to get a javax.swing.JComponent object that we can place in our application's GUI. Yep, a JComponent is returned. That means that you can create movies on JButtons, JLabels, JMenus, as well as the expected JPanels.
As you can see from the following code grabbed from our QTJViewer class, we create a quicktime.QTFile object using our filename String. From there, the quicktime.std.movies.Movie object's static fromFile method is used to load the movie file into a quicktime.std.movies.Movie object.
private JComponent getMovieScreen(String file) { try { QTFile qtFile = new QTFile(file); OpenMovieFile openMovieFile = OpenMovieFile.asRead(qtFile); movie = Movie.fromFile(openMovieFile); MoviePlayer moviePlayer = new MoviePlayer(movie); return QTFactory.makeQTJComponent(moviePlayer).asJComponent(); } catch (Exception ex) { ex.printStackTrace(); return new JLabel("ERROR"); } } }
We aren't allowed to pass the fromFile method our QTFile object directly; the method expects a quicktime.io.OpenMovieFile object, which we can obtain with this call:
OpenMovieFile openMovieFile = OpenMovieFile.asRead(qtFile);
Remember that our motivation was to get a JComponent that we could place in the application GUI. To do this, we have to make a quicktime.app.view.MoviePlayer object from our quicktime.std.movies.Movie object:
MoviePlayer moviePlayer = new MoviePlayer(movie);
Our JComponent is finally created via a call to the quicktime.app.view.QTFactory's makeQTJComponent method, which takes in our newly created MoviePlayer object:
QTFactory.makeQTJComponent(moviePlayer.asJComponent());
The resulting JComponent object is returned by our custom getMovieScreen method.