FileDocCategorySizeDatePackage
SimpleMoviePlayer.javaAPI DocExample12205Tue Dec 17 03:04:24 GMT 2002com.wiverson.macosbook.quicktime

SimpleMoviePlayer

public class SimpleMoviePlayer extends JFrame implements quicktime.io.IOConstants, quicktime.std.StdQTConstants, quicktime.Errors

Fields Summary
JButton
importButton
JButton
referenceButton
JButton
exportButton
JPanel
commandPanel
JLabel
statusLabel
quicktime.app.display.QTCanvas
myQTCanvas
Constructors Summary
SimpleMoviePlayer(String title)

    
    /* 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.
     */
     
    
        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);
            }
        });
    
Methods Summary
voiddisplayMovie(quicktime.std.movies.Movie m)

        //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();
    
voidexportMovie()

        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();
        }
    
voidexportMovie(quicktime.io.QTFile movieFile)

        // 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();
    
public voidimportMedia()

        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)
        {}
    
public static voidmain(java.lang.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();
        }
    
voidmakeReferenceMovie()

        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();
        }
    
voidmakeReferenceMovie(quicktime.io.QTFile movieFile, java.lang.String outputPath)

        
        // 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);