本文的目的是為讀者提供處理不同情況的代碼,您可以參考MMAPI DOC。
Player p;
TempoControl tc;
try {
p = Manager.createPlayer("http://webserver/tune.mid");
p.realize(); // Grab the tempo control.
tc = (TempoControl)p.getControl("TempoControl");
tc.setTempo(120000); // 120 beats/min
p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}
Player p;
VideoControl vc;
try {
p = Manager.createPlayer("http://webserver/movIE.mpg");
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();
} catch (IOException ioe) {
} catch (MediaException me) {
}
RecordStore rs;
int recordID; : // code to set up the record store.
try {
InputStream is = new ByteArrayInputStream(rs.getRecord(recordID));
Player p = Manager.createPlayer(is, "audio/X-wav");
p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}
/** Notice that in MIDP 2.0, the wav format is mandatory only *//** in the case that the device supports sampled audio. */
try {
InputStream is = getClass().getResourceAsStream("audio.wav");
Player p = Manager.createPlayer(is, "audio/X-wav");
p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}
Player p1, p2;
try {
p1 = Manager.createPlayer("http://webserver/tune.mid");
p1.realize();
p2 = Manager.createPlayer("http://webserver/movIE.mpg");
p2.realize();
p2.setTimeBase(p1.getTimeBase());
p1.prefetch();
p2.prefetch();
p1.start();
p2.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}
try { // Create a DataSource that captures live audio.
Player p = Manager.createPlayer("capture://audio");
p.realize(); // Get the RecordControl, set the record location, and
// start the Player and record for 5 seconds.
RecordControl rc = (RecordControl)p.getControl("RecordControl");
rc.setRecordLocation("file:/tmp/audio.wav");
rc.startRecord();
p.start();
Thread.currentThread().sleep(5000);
p.stop();
rc.stopRecord();
rc.commit();
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException e) {
}
實現攝像功能
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();
} catch (IOException ioe) {
} catch (MediaException me) {
}// now take a picturetry { byte[] pngImage = vc.getSnapshot(null);
// do something with the image ...}
catch (MediaException me) { }
在後面的文章中我們將通過完整的實例演示如何使用MMAPI開發應用程序。