package com.wiverson.macosbook.quicktime;
import quicktime.std.movies.Movie;
import java.awt.event.*;
import quicktime.QTException;
import quicktime.io.QTFile;
import java.awt.FileDialog;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JDialog;
public class SimpleMoviePlayer extends javax.swing.JFrame
implements quicktime.std.StdQTConstants, quicktime.Errors, quicktime.io.IOConstants
{
public static void main(String args[])
{
try
{
// Required to initialize the QuickTime environment.
// Performs checks to ensure QuickTime is installed and
// also loads and sets up QuickTime.
quicktime.QTSession.open();
SimpleMoviePlayer myPlayer = new SimpleMoviePlayer("Simple Player");
myPlayer.pack();
myPlayer.show();
myPlayer.toFront();
} catch (Exception e)
{
e.printStackTrace();
quicktime.QTSession.close();
}
}
JButton importButton = new JButton("Import Media...");
JButton referenceButton = new JButton("Export Reference Media...");
JButton exportButton = new JButton("Export Full Media...");
JPanel commandPanel = new JPanel();
JLabel statusLabel = new JLabel("Ready.");
// The QTCanvas is the "heavy lifter" QuickTime component
// that does all the hard work for punching the QuickTime
// viewer through and into the JFrame.
quicktime.app.display.QTCanvas myQTCanvas;
/* Creates the application user interface. You'll
*notice that there is no reference to new user interface
*options for QuickTime features - those are controlled
*the "punched through" QuickTime capabilities.
*/
SimpleMoviePlayer(String title)
{
super(title);
getContentPane().add(statusLabel, "North");
importButton.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent ae)
{
importMedia();
}
});
commandPanel.add(importButton);
referenceButton.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent ae)
{
makeReferenceMovie();
}
});
commandPanel.add(referenceButton);
exportButton.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent ae)
{
exportMovie();
}
});
commandPanel.add(exportButton);
this.getContentPane().add(commandPanel, "South");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// Go ahead and clean up the QuickTime layer
quicktime.QTSession.close();
dispose();
}
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
}
public void importMedia()
{
try
{
//
FileDialog myFileDialog = new FileDialog(this, "Choose Media to Import...", FileDialog.LOAD);
myFileDialog.show();
if (myFileDialog.getFile() == null)
return;
QTFile importFile = new QTFile(myFileDialog.getDirectory() + myFileDialog.getFile());
// You can import any supported media type into QuickTime using
// the QTFactory.
// QTFactory.makeDrawable() methods take a variety of inputs,
// from URLs to an InputStream, and produce a usable media object.
// The media object might represent a sound file (such as an MP3),
// a picture (such as a GIF), or a full movie (such as a
// QuickTime .mov)
quicktime.app.display.QTDrawable media = null;
try
{
media = quicktime.app.QTFactory.makeDrawable(importFile);
} catch (quicktime.QTException qtException)
{
// If not a user cancel, go ahead and report error
if(qtException.getMessage().indexOf("cantFindHandler") > 0)
qtException.printStackTrace();
else
{
java.awt.Toolkit.getDefaultToolkit().beep();
statusLabel.setText("Unable to open this file type.");
}
}
if(media != null)
{
if (myQTCanvas == null)
{
myQTCanvas = new quicktime.app.display.QTCanvas();
this.getContentPane().add(myQTCanvas, "Center");
}
myQTCanvas.setClient(media, true);
statusLabel.setText(importFile.getPath());
// This resizes the UI. Note that the "preferred" size
// for myQTCanvas has been changed to whatever works for
// the player.
pack();
}
} catch (QTException err)
{
if (err.errorCode() == userCanceledErr) return;
err.printStackTrace();
} catch (java.io.IOException ie)
{}
}
void displayMovie(Movie m) throws QTException
{
//make a QTPlayer out of the Movie and set it as the client of the QTCanvas
quicktime.app.players.QTPlayer p = new quicktime.app.players.QTPlayer(new quicktime.std.movies.MovieController(m));
if (myQTCanvas == null)
{
myQTCanvas = new quicktime.app.display.QTCanvas();
getContentPane().add(myQTCanvas, "Center");
}
myQTCanvas.setClient(p, true);
pack();
}
void makeReferenceMovie()
{
try
{
FileDialog rfd = new FileDialog(this, "Choose Movie to Reference...", FileDialog.LOAD);
rfd.show();
if (rfd.getFile() == null)
return;
QTFile movieFile = new QTFile(rfd.getDirectory() + rfd.getFile());
FileDialog ofd = new FileDialog(this, "New Movie to create...", FileDialog.SAVE);
ofd.show();
if (ofd.getFile() == null)
{
return;
}
makeReferenceMovie(movieFile, ofd.getDirectory() + ofd.getFile());
} catch (QTException err)
{
if (err.errorCode() == userCanceledErr)
return;
err.printStackTrace();
}
}
//makes a new movie that reference the data in an existing movie
void makeReferenceMovie(QTFile movieFile, String outputPath) throws QTException
{
// Create the movie object from the original movie
Movie theMovie = Movie.fromFile(quicktime.io.OpenMovieFile.asRead(movieFile));
displayMovie(theMovie);
QTFile outputMovie = new QTFile(outputPath);
//shortcut movies are movies that just contain a reference
//to another movie. It can begin to be complicated for users
//to track which is which - you may wish to expose a flag
//indicating handles to movies as opposed to flattened
//movies in your user interface.
//make a Data ref out of a URL that references the movie
quicktime.std.movies.media.DataRef targetDataRef = new quicktime.std.movies.media.DataRef("file://" + movieFile.getPath());
//make the very small short cut movie
outputMovie.createShortcutMovieFile(kMoviePlayer, smSystemScript, createMovieFileDeleteCurFile, targetDataRef);
}
void exportMovie()
{
try
{
FileDialog efd = new FileDialog(this, "Choose Movie to Export...", FileDialog.LOAD);
efd.show();
if (efd.getFile() == null)
return;
QTFile movieFile = new QTFile(efd.getDirectory() + efd.getFile());
exportMovie(movieFile);
} catch (QTException err)
{
err.printStackTrace();
}
}
//export (to a movie) the incoming movie
//user dialog allows user to customise media formats and tracks that are exported
void exportMovie(QTFile movieFile) throws QTException
{
// Create the movie object from the original movie
Movie theMovie = Movie.fromFile(quicktime.io.OpenMovieFile.asRead(movieFile));
displayMovie(theMovie);
//we do this in a different thread because exporting can take some time
// and the event thread should not be blocked for so long... but it tends
// to really drag the UI anyways on Mac OS X for serious exporting.
new Thread(new com.wiverson.macosbook.quicktime.SimpleMoviePlayer.Runner(theMovie, statusLabel)).start();
}
static class Runner implements Runnable
{
Runner(Movie mov, JLabel inStatus)
{
theInputMovie = mov;
status = inStatus;
}
Movie theInputMovie;
JLabel status;
public void run()
{
try
{
//this determines both the exporter type, the resulting file type.
// thus one could specify this to be AIFF and the resulting file will be an AIFF file
// - in this case the result will be a movie.
int exportType = kQTFileTypeMovie;
//an application can alternatively configure exporter through setting
// up the exporter in code to conform to the format appropriate
FileDialog ofd = new FileDialog(new Frame(), "Export Movie to...", FileDialog.SAVE);
ofd.show();
if (ofd.getFile() == null)
return;
QTFile outFile = new QTFile(ofd.getDirectory() + ofd.getFile());
// Create a movie exporter so we can customise its settings
// this could also be used in the convertToFile version, but
// if we don't have custom settings then we allow the convertToFile
// to create the exporter for us - based on the exportType we pass to it.
quicktime.std.qtcomponents.MovieExporter theMovieExp = new quicktime.std.qtcomponents.MovieExporter(exportType);
// Set export settings from the user.
theMovieExp.doUserDialog(theInputMovie, null, 0, theInputMovie.getDuration());
//this returns a dupFNErr on windows and is also more work for the application
// create the output file but don't open it
outFile.createMovieFile(kMoviePlayer, createMovieFileDeleteCurFile);
status.setText("Starting export...");
// do the export of the movie
theMovieExp.toFile(outFile, theInputMovie, null, 0, theInputMovie.getDuration());
status.setText("Export complete.");
} catch (QTException e)
{
e.printStackTrace();
}
}
}
}
|