next up previous
Next: Using The Detached Controller Up: Displaying and Streaming Movies Previous: Displaying and Streaming Movies

Playing a Streaming Movie

This example builds on the QTSimpleApplet code discussed in ``The QTSimpleApplet Code''. This applet enables you to play a steaming movie from a URL.

You define the instance variables for the applet:

private Drawable myQTContent;
private QTCanvas myQTCanvas;

Just as with the QTSimpleApplet code sample, you can use the standard init(), start(), stop(), and destroy() methods to initialize, execute, and terminate the applet. Likewise, you call QTSession.open()in order to make sure that QuickTime is present and initialized. Again, this is a required call before any QuickTime for Java classes can be used. It is called first in the init() method. In order to shut down QuickTime properly, you also need to call QTSession.close() if you have previously called QTSession.open(). This is called in the destroy() method.

QTCanvas, as we've seen, is a display space into which QuickTime can draw and receive events. QTCanvas provides the output destination for QuickTime drawing. You set up a QTCanvas to display its content at its original size or smaller and centered in the space given to the QTCanvas when the applet is laid out. The QTCanvas is initialized to display its client up to as large as that client's initial size. And 0.5F flags are used to position the canvas at the center of the space allocated to it by its parent container's layout manager:

setLayout (new BorderLayout());
myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
add (myQTCanvas, "Center");

You need to set the client as a Drawable object that can display into the canvas. The QuickTime logo is displayed when there is no movie to display. Thus, ImageDrawer is set up as the initial client of QTCanvas.

myQTContent = ImageDrawer.getQTLogo();

You enter the URL to a QuickTime movie to be displayed in a text field:

final TextField urlTextField = new TextField (
"file:///... Enter an URL to a movie",
30);

You set the font and font size in the text field for the URL. The initial string is displayed in the text field. You add an ActionListener so that the events taking place on the text field are captured and executed. tf.getText() returns the URL that the user has entered:

urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
urlTextField.setEditable (true);
urlTextField.addActionListener (new ActionListener () {
TextField tf = urlTextField;
public void actionPerformed (ActionEvent ae) {
myQTContent = QTFactory.makeDrawable (tf.getText());
myQTCanvas.setClient (myQTContent, true);
}
});

The URL can support the usual protocols:

The URL can also point to any media file (movies, images, and so on) that QuickTime can present. The single makeDrawable() method will return the appropriate QuickTime object to present the specified media. Once created, this QuickTime object is set as the client of the QTCanvas and can then be viewed and/or played by the user.

Note that no error handling is done in the code in this example to keep things simple.

The full code for the QTStreamingApplet.java is given below:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import quicktime.*;
import quicktime.io.QTFile;

import quicktime.app.QTFactory;
import quicktime.app.display.*;
import quicktime.app.image.ImageDrawer;

public class QTStreamingApplet extends Applet {

	private Drawable myQTContent;
	private QTCanvas myQTCanvas;
    private Vector urlTable;
    private TextField urlTextField;
    private PopupMenu pm;
    
	public void init () {
		try {
			QTSession.open();
				// set up a QTCanvas which will disply its content 
				// at its original size of smaller and centered in the space given
				// to the QTCanvas when the applet is layed out
			setLayout (new BorderLayout());
			urlTable = new Vector();
			myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
			add (myQTCanvas, "Center");		
			
			myQTContent = ImageDrawer.getQTLogo();
		
			add (bottomPanel(), "South");
			readURL();
						
		} catch (QTException qtE) {
				//in this case the only QTException is in QTSession.open()
			throw new RuntimeException (qtE.getMessage());		
		}
	}	
	
	public void start () { 
		try { // if QT was not successfully initialized the QTCanvas will be null
			if (myQTCanvas != null)
				myQTCanvas.setClient (myQTContent, true);			
		} catch (QTException e) {
			e.printStackTrace();
		}
	}
	
	public void stop () { 
		if (myQTCanvas != null)
			myQTCanvas.removeClient();
	}
	
	public void destroy () {
		QTSession.close();
	}
		
		
	/**
	 * creates drawable object from the URL the user has entered
	 * or from the menu item chosen
	 */
	private void createNewMovieFromURL(String selURL) throws QTException {
		String url = urlTextField.getText();	
		myQTContent = QTFactory.makeDrawable (url);
		myQTCanvas.setClient (myQTContent, true);
	}

	/**
	 * the parameter list in the html file should 
  * have a name = total having the value of the total
	 * number of url's to be appended to the popup menu.
  * Each url should start with "url" + i , where i
	 * is 1, 2.... in increasing order till the i = total.
	 */
	private void readURL() throws QTException{
		for ( int i = 0; i < Integer.parseInt(getParameter("total")); i++) {
			String url = getParameter("url" + (i + 1));		
			if ( url != null) 
				appendItem(url);
		}
	}
	
	public Component bottomPanel () {
		Panel row2 =  new Panel();
		row2.setLayout(new FlowLayout(FlowLayout.CENTER));
		row2.setBackground(Color.white);	
			
		urlTextField = new TextField ("file:///... Enter an URL to a movie", 40);
         //Enter URL to movie here", 30);
		urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
		urlTextField.setEditable (true);
		urlTextField.addActionListener (new ActionListener () {
			//TextField tf = urlTextField;
			
			public void actionPerformed (ActionEvent ae) {
				if (myQTCanvas != null) {
					try {
						createNewMovieFromURL(urlTextField.getText());
						appendItem(urlTextField.getText());
					} catch (QTException e) {
							//probably a non-fatal error that the Applet 
							//should deal with more informatively
						e.printStackTrace();
					}
				}
			}
		});

		row2.add(urlTextField);
		
		pm = new PopupMenu();
 		
		Image img1 = Toolkit.getDefaultToolkit().getImage(
         getCodeBase().getFile() + "images/p1.gif");
		Image img2 = Toolkit.getDefaultToolkit().getImage(
         getCodeBase().getFile() + "images/r1.gif");
		IconButton menuButton = 
       new PopupMenuButton (img1, img2, pm);
					
		row2.add (menuButton);
		
		return row2;
	}
	
	/** 
	 * This appends the input movie url entered in the textfield or
	 * selected from the Choose movie menu to the PopupMenu. It checks
	 * for its duplicates and then appends it to the list in the popupmenu
     * @param     urlItem  Item to be appended to the PopupMenu
	 */
    private void appendItem(String urlItem) throws QTException {
    	if (urlTable.contains(urlItem) == false) {
    		MenuItem item = new MenuItem(urlItem);
        	pm.add (item);
        	item.addActionListener (new ActionListener () {
        		public void actionPerformed(ActionEvent event) {
        			try {
					MenuItem jm = (MenuItem)event.getSource();
					String selURL = jm.getLabel();

					urlTextField.setText(selURL);
					createNewMovieFromURL(selURL);
					}catch (QTException ex) {
						ex.printStackTrace();
					}
				}	
			});
        	urlTable.addElement(urlItem);
        }	
    }
}

For supporting code required for this example (IconButton.java and
PopupMenuButton.java refer to the sample code folder online for this example.


next up previous
Next: Using The Detached Controller Up: Displaying and Streaming Movies Previous: Displaying and Streaming Movies
Dave Marshall
10/4/2001