Java Reference
In-Depth Information
Class Player from the JMF is capable of playing all the audio formats already
mentioned, as well as a number of others. In addition, it can play a variety of video
formats, such as AV I ( .avi fi les), GSM ( .gsm fi les), MPEG-1 ( .mpg fi les) and Apple
QuickTime ( .mov fi les). (Unfortunately, it does not also display image fi les.) This
class is held in package , which should be imported into application
programs. The basic steps required for a program that is to play a sound or video fi le
are given below.
1. Accept a fi le name (including the path, if the fi le is not in the same directory as
the program) and create a File object, supplying the fi le name as the constructor's
single argument.
2. Use the File class's exists method to check that the fi le exists.
3. Create a Player object via static method createPlayer of class Manager (also
from package ). This method takes a single URL argument that can
be generated via the File class's toURL method.
4. Use the exception-handling mechanism (catching any Exception object) to check
that the fi le is of a valid type.
5. Provide a ControllerListener (package ) for the media player.
6. Supply a defi nition for method controllerUpdate of the above ControllerListener
object. This method (which takes a ControllerEvent argument) will usually
generate any required visual and/or control panel components via Player methods
getVisualComponent and getControlPanelComponent and then add those
components to the content pane. As its last step, it should execute the doLayout
method on the content pane.
7. Execute the Player object's start method.
Class ControllerEvent actually has 21 (!) direct and indirect subclasses,
but the one that is likely to be of most use is class RealizeCompleteEvent . This is
the type of object passed to controllerUpdate when the Player object has deter-
mined the clip's medium type and has loaded the clip. Inbuilt operator instan-
ceof may be used to check the specifi c subtype of the ControllerEvent object
that has been generated. Method getVisualComponent will return null for an
audio clip (since an audio clip has no associated display component) and non-
null for a video clip.
The following program creates a Player object that plays any audio or video clip for
which the name is entered by the user. As ever, the program lines corresponding to
the above steps are indicated by emboldened comments that specify the associated
step numbers.
Note that the JMF API has not been updated to allow adding of its GUI components
directly to the application frame, as was introduced for all AWT and Swing compo-
nents in J2SE 5.0. Instead, it is necessary to get a reference to the application's content
pane (as a Container reference) and add GUI components that are part of the JMF
to the content pane (as was the practice for all GUI components before Java 5). All
other GUI components can, of course, be added directly to the application frame.
Search WWH ::

Custom Search