Java Reference
In-Depth Information
be decoded and played. This process takes some time to complete when
the data is in RMS, a JAR file or a regular file accessed with the File Connec-
tion API. It is also possible that the data resides in the network; in this case
we are again subject to network conditions that can affect speed and relia-
bility of I/O operations, as discussed in previous sections. The most imme-
diate implication is that, in general, all your operations with Player s,
especially creation, realization and pre-fetching, should be done on a
separate thread, to prevent these operations from freezing a system thread.
Keep in mind that this is valid not only for reading, but also for writing
data. Let us look at the following excerpt from our previous example:
//setup recording
record = (RecordControl) player.getControl("RecordControl");
record.setRecordSizeLimit(300000);
conn = (FileConnection) Connector.open(PATH, Connector.READ_WRITE);
if (!conn.exists())
conn.create();
stream = conn.openOutputStream();
record.setRecordStream(stream);
Here we are recording video from the camera and sending it to a file,
up to a limit of 300,000 bytes (roughly 292 KB). A real-world application,
however, would not have such a hard-coded limit, as it would severely
impact its functionality. A more realistic approach would be to record
until the user decides to stop or the disk is full. In either case, there might
be several megabytes of video in the memory buffer waiting to be flushed
to the disk when RecordControl.commit() is called. This operation
should also be moved to a separate thread, as writing millions of bytes to
the disk will take some time and doing so in the system thread will most
certainly hang the application from the user's standpoint.
While in principle those cases should be self-evident, at times they
may be hidden in code sections that apparently don't perform any heavy
operations. Consider this example for capturing a snapshot with the
device's camera:
Player p;
VideoControl vc;
// initialize camera
try p = Manager.createPlayer("capture://video");
p.realize();
// Grab the video control and set it to the current display.
vc = (VideoControl)p.getControl("VideoControl");
if(vc != null) {
Form form = new Form("video");
form.append((Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null));
Display.getDisplay(midlet).setCurrent(form);
} p.start();
}
Search WWH ::




Custom Search