Java Media Framework - Code Example

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:

    “Many people will say to working mothers, in effect, “I don’t think you can have it all.” The phrase for “have it all” is code for “have your cake and eat it too.” What these people really mean is that achievement in the workplace has always come at a price—usually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.”
    —Anne C. Weisberg (20th century)

    “... 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–?)