Java Reference
In-Depth Information
You might think you don't need this information—after all, the vendor information
that accompanies most Java ME devices provides a list of supported media types—but
frequently you do, because there's no guarantee that a Java ME device's firmware is wired
correctly with the MMAPI just because it can render a media type using a native applica-
tion. In fact, it's possible at times that the media list that the MMAPI Manager provides
may be incorrect.
Tip While testing any Java ME application on a device is a crucial part of your development cycle,
it's especially so with most of the Java ME extensions documented through JSRs, and the MMAPI is no
different. Always test your application on target hardware early in your development cycle, and repeat the
testing often.
The heart of the UI flow is the commandListener method and the MIDlet fields mediaName
and mediaType . When you choose a kind of media to play, commandListener uses a simple
switch statement to populate these variables with the file name in the JAR file and the
type of media you selected. Next, it launches the player thread, using the MIDlet class
itself as the Runnable item. The resulting thread starts with the MIDlet class's run method,
which initializes and starts playback.
The next sections describe how the MIDlet plays audio and video, captures images
from the video stream, and plays animated SVG content.
Playing Audio and Video
When the player thread starts, its run method first invokes playFromResource . This method
uses the information the commandListener set aside about the media type you selected.
Later, the implementation uses this information to determine whether to initialize the
SVGAPI or the MMAPI, as well as to set the next Displayable item in the viewerDisplayable
field of the MIDlet. In addition, if you selected a file that isn't an SVGAPI, the class will
create any additional controls the MIDlet requires. This logic consists of a series of nested
if - else statements, which are sufficient for this example, but possibly worth dividing into
separate subclasses with a common interface that returns a Displayable .
The initMediaPlayer does the heavy lifting initializing the MMAPI; it closely resem-
bles Listing 16-1. It creates a Form instance, which becomes the Displayable that the
MIDlet shows when rendering the media. initMediaPlayer adds an instance of Command
that permits you to exit the application before setting the command listener for the new
form to the MIDlet.
The method then creates a Player instance, using either the locator of the camera if
you selected the Camera Capture option, or an InputStream taken from the JAR resource
named by your choice in the commandAction method. Finally, it configures the new Player
instance by first realizing the player and then prefetching the content.
 
Search WWH ::




Custom Search