当前页面: 开发资料首页 → J2ME 专题 → 熟练使用J2ME可选包MMAPI
摘要: 熟练使用J2ME可选包MMAPI
作者:mingjava 文章来源:J2ME开发网
<table class="main_tdbg_575" style="WORD-BREAK: break-all" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tr><td colspan="2"></td></tr><tr><td id="fontzoom" style="WORD-BREAK: break-all" valign="top" colspan="2" height="600">了解J2ME可选包MMAPI一文向读者介绍了MMAPI的基本知识,掌握MMAPI的灵活性应该是重点。本文将讲述如何在实际开发中使用MMAPI。
本文的目的是为读者提供处理不同情况的代码,您可以参考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 picture try { byte[] pngImage = vc.getSnapshot(null); // do something with the image ... } catch (MediaException me) { }
在后面的文章中我们将通过完整的实例演示如何使用MMAPI开发应用程序。