Code Example
The following example shows an AWT file-selection dialog, and attempts to load and play the media file selected by the user.
import javax.media.*; import java.io.File; import java.awt.*; public class TrivialJMFPlayer extends Frame { public static void main (String args) { try { Frame f = new TrivialJMFPlayer; f.pack; f.setVisible (true); } catch (Exception e) { e.printStackTrace; } } public TrivialJMFPlayer throws java.io.IOException, java.net.MalformedURLException, javax.media.MediaException { FileDialog fd = new FileDialog (this, "TrivialJMFPlayer", FileDialog.LOAD); fd.setVisible(true); File f = new File(fd.getDirectory, fd.getFile); Player p = Manager.createRealizedPlayer (f.toURI.toURL); Component c = p.getVisualComponent; add(c); p.start; } }Much of the example is involved with the building of the AWT GUI. Only two lines touch JMF. Manager.createRealizedPlayer implicitly creates a DataSource from a URL representation of the file, creates a Player, and realizes it, meaning that all resources necessary for playback are allocated and initialized. The getVisualComponent asks the Player for an AWT Component suitable for use in a GUI. If a control component were desired, it would be obtained with getControlPanelComponent and added to the GUI separately. Note that the developer is not concerned with the format of the media - this code works equally well for any media format that JMF can import and read.
Read more about this topic: Java Media Framework
Famous quotes containing the word code:
“Acknowledge your will and speak to us all, This alone is what I will to be! Hang your own penal code up above you: we want to be its enforcers!”
—Friedrich Nietzsche (18441900)
“... the self respect of individuals ought to make them demand of their leaders conformity with an agreed-upon code of ethics and moral conduct.”
—Mary Barnett Gilson (1877?)