Java Reference
In-Depth Information
Once
initMediaPlayer
does its work, control returns to
playFromResource
, which
invokes either
configViewSound
or
configViewVideo
to do any additional postconfiguration
of the user interface before starting the media playback. The
configViewSound
method
does nothing; it's simply a placeholder. You might want to experiment with the code and
create various
Control
subclasses on the
player
field here, such as a
VolumeControl
.
configViewVideo
, however, must do a little work; it creates another
Command
instance
that lets you trigger a video capture and adds it to the
Form
instance before creating the
VideoControl
instance. The resulting
VideoControl
provides the method with an
Item
instance to add to the
Form
. Finally, the method creates a second additional empty
ImageItem
instance that the MIDlet uses to display the image you capture when you
select the Capture option.
The
playFromResource
method then starts the newly created
Player
instance and sets
the current
Viewable
instance to the
viewerDisplayable
before returning control to the
player thread's
run
method. At this point in the MIDlet execution, the MMAPI renders the
media you selected, while the player thread blocks on
this
waiting for you to select the
Capture option.
Capturing Images
The
VideoControl
instance that the MIDlet stores in the
videoControl
field can capture a
snapshot once its
Player
instance has begun playback, although it's inadvisable to do so
on the main thread, because that can stall either the MIDlet UI or the MMAPI itself. By
using the MIDlet instance itself as an explicit lock via the Java
synchronized
keyword and
the
Object
method
notify
, the MIDlet can signal from the UI thread to the player thread
that you want to capture a frame. In addition, using the
synchronized
keyword ensures
that you can't trigger a capture while the application is performing a capture, which is
probably an error anyway.
Once the Java VM uses
notify
/
synchronized
to kick the player thread to capture a
frame, the player thread performs the capture by invoking the
videoControl
's
getSnapshot
method, specifying the default encoding. At this point, the player has a
byte[]
of the cap-
tured image data; the code just uses the data to set the other image on the
Form
to display
the image you captured.
Another thing you might want to do with image data that your application has cap-
tured is to share it with someone else. You could do that by changing the
MMSSender
class I
show you in Chapter 14 to take an optional
byte[]
of encoded image data, as you see in
Listing 16-18, and using the new
MMSSender
method that Listing 16-18 defines.