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:
“Motion or change, and identity or rest, are the first and second secrets of nature: Motion and Rest. The whole code of her laws may be written on the thumbnail, or the signet of a ring.”
—Ralph Waldo Emerson (18031882)
“Faultless honesty is a sine qua non of business life. Not alone the honesty according to the moral code and the Bible. When I speak of honesty I refer to the small, hidden, evasive meannesses of our natures. I speak of the honesty of ourselves to ourselves.”
—Alice Foote MacDougall (18671945)