| packageorg.wikipedia.examples;
importjava.awt.Component;
importjava.awt.FileDialog;
importjava.awt.Frame;
importjava.io.File;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjavax.media.Manager;
importjavax.media.Player;
importjavax.media.MediaException;
publicclass TrivialJMFPlayerextendsFrame{
publicTrivialJMFPlayer()throwsIOException,MalformedURLException,MediaException{
FileDialogfd=newFileDialog(this,"TrivialJMFPlayer",FileDialog.LOAD);
fd.setVisible(true);
Filef=newFile(fd.getDirectory(),fd.getFile());
Playerp=Manager.createRealizedPlayer(f.toURI().toURL());
Componentc=p.getVisualComponent();
add(c);
p.start();
}
publicstaticvoidmain(String[]args){
try{
Framef=newTrivialJMFPlayer();
f.pack();
f.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
}
Much of the example is involved with the building of the AWT GUI. Only two lines touch JMF. Manager.createRealizedPlayer() implicitly creates a javax.media.protocol.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 javax.media.Player for a java.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.
|