分享
 
 
 

熟练使用J2ME可选包MMAPI

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

了解J2ME可选包MMAPI一文向读者介绍了MMAPI的基本知识,掌握MMAPI的灵活性应该是重点。本文将讲述如何在实际开发中使用MMAPI。

本文的目的是为读者提供处理不同情况的代码,您可以参考MMAPI DOC。

播放单音

try {

Manager.playTone(ToneControl.C4, 5000 /* millisec */, 100 /* max vol */);

} catch (MediaException e) { }

简单媒体重放功能实现

try {

Player p = Manager.createPlayer("http://webserver/music.mp3");

p.setLoopCount(5);

p.start();

} catch (IOException ioe) {

} catch (MediaException me) { }

详细重放控制

static final long SECS_TO_MICROSECS = 1000000L;

Player p;

VolumeControl vc;

try {

p = Manager.createPlayer("http://webserver/music.mp3");

p.realize();

// Set a listener.

p.addPlayerListener(new Listener());

// Grab volume control for the player.

// Set Volume to max.

vc = (VolumeControl)p.getControl("VolumeControl");

if (vc != null)

vc.setLevel(100);

// Set a start time.

p.setMediaTime(5 * SECS_TO_MICROSECS);

// Guarantee that the player can start with the smallest latency.

p.prefetch();

// Non-blocking start

p.start();

} catch (IOException ioe) {

} catch (MediaException me) { }

class Listener implements PlayerListener {

public void playerUpdate(Player p, String event, Object eventData) {

if (event == END_OF_MEDIA event == STOP_AT_TIME) {

System.out.println("Done processing");

try {

p.setMediaTime(5 * SECS_TO_MICROSECS);

p.start();

} catch (MediaException me) { }

break;

}

}

}

实现MIDI重放控制

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) { }

播放RMS内存储的数据

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) { }

播放Jar文件中存储的媒体

/** 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的同步

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) { }

产生单音序列

byte tempo = 30; // set tempo to 120 bpm

byte d = 8; // eighth-note

byte C4 = ToneControl.C4;;

byte D4 = (byte)(C4 + 2); // a whole step

byte E4 = (byte)(C4 + 4); // a major third

byte G4 = (byte)(C4 + 7); // a fifth

byte rest = ToneControl.SILENCE; // rest

byte[] mySequence = {

ToneControl.VERSION, 1, // version 1

ToneControl.TEMPO, tempo, // set tempo

ToneControl.BLOCK_START, 0, // start define "A" section

E4,d, D4,d, C4,d, E4,d, // content of "A" section

E4,d, E4,d, E4,d, rest,d,

ToneControl.BLOCK_END, 0, // end define "A" section

ToneControl.PLAY_BLOCK, 0, // play "A" section

D4,d, D4,d, D4,d, rest,d, // play "B" section

E4,d, G4,d, G4,d, rest,d,

ToneControl.PLAY_BLOCK, 0, // repeat "A" section

D4,d, D4,d, E4,d, D4,d, C4,d // play "C" section

};

try{

Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);

p.realize();

ToneControl c = (ToneControl)p.getControl("ToneControl");

c.setSequence(mySequence);

p.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开发应用程序。

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有