FileDocCategorySizeDatePackage
AudioVideoCaptureToDisk.javaAPI DocExample4962Wed Nov 10 13:01:08 GMT 2004com.oreilly.qtjnotebook.ch06

AudioVideoCaptureToDisk

public class AudioVideoCaptureToDisk extends Frame implements ActionListener

Fields Summary
SequenceGrabber
grabber
SGVideoChannel
videoChannel
SGSoundChannel
soundChannel
QDGraphics
gw
QDRect
grabBounds
boolean
grabbing
Button
stopButton
QTFile
grabFile
Constructors Summary
public AudioVideoCaptureToDisk()

        super ("Audio/Video Capture");
        QTSessionCheck.check();
        setLayout (new GridLayout (2, 1));
        add (new Label ("Capturing video..."));
        stopButton = new Button ("Stop");
        stopButton.addActionListener (this);
        add (stopButton);
        setUpVideoGrab();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (e.getSource() == stopButton) {
            System.out.println ("Stop grabbing");
            try {
                grabbing = false;
                if (grabber != null) {
                    grabber.stop();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                System.exit (0);
            }
        }
    
public static voidmain(java.lang.String[] args)

        try {
            Frame f = new AudioVideoCaptureToDisk();
            f.pack();
            f.setVisible(true);
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
protected voidsetUpVideoGrab()

        grabber = new SequenceGrabber();
        System.out.println ("got grabber");

        // force an offscreen gworld
        grabBounds = new QDRect (320, 240);
        gw = new QDGraphics (grabBounds);
        grabber.setGWorld (gw, null);

        // get videoChannel and set its bounds
        videoChannel = new SGVideoChannel (grabber);
        soundChannel = new SGSoundChannel (grabber);
        System.out.println ("Got audio, video channels");
        videoChannel.setBounds (grabBounds);

        // get settings
        // yikes! these often crash java 1.4.2 on mac os x!
        // see book for workaround (call these from non-AWT thread and block AWT on it)
        // soundChannel.settingsDialog();
        // videoChannel.settingsDialog();

        // prepare and start previewing
        videoChannel.setUsage (StdQTConstants.seqGrabRecord);
        soundChannel.setUsage (StdQTConstants.seqGrabPreview |
                               StdQTConstants.seqGrabRecord);
        soundChannel.setVolume (0.0f);
        grabber.prepare(false, true);
        grabber.startPreview();

        // create output file
        grabFile = new QTFile (new java.io.File ("audiovideograb.mov"));
        grabber.setDataOutput(grabFile,
                              StdQTConstants.seqGrabToDisk 
                              //seqGrabDontAddMovieResource);
                              );
        grabber.startRecord();

        grabbing = true;

        // set up thread to idle
        ActionListener timerCallback =
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (grabbing) {
                        try {
                            System.out.println ("idle");
                            grabber.idle();
                            grabber.update(null);
                        } catch (QTException qte) {
                            qte.printStackTrace();
                        }
                    }
                }
            };
        Timer timer = new Timer (50, timerCallback);
        timer.start();