栏目分类
开源技术 | Hibernate | JUnit | Tomcat | JBoss | eclipse | Spring | ANT | Struts
java新手入门 | 基础入门 | 开发工具 | JDK | oop面向对象 | 安全配置
J2EE | mvcwea | 应用服务器 | Rmi/Corba/Jini | J2ee核心 | Servlet/JSP | EJB | JDBC/JDO
J2ME | 核心技术 | 嵌入式 | 无线开发
XML | WebServices | XMLBeans
高级技术 | 多线程 | 设计模式 | ULM/OO | p2p/Jxta | JavaSecurity
核心技术 | SwingAwtApplet | 高级编程 | 网络编程 | Jvm技术 | JavaMedia
其它技术 | 资讯动态
排行榜
·J2ME基础入门教程
·熟练使用J2ME在实际开发中的可选包MMAPI
·新手入门之j2me学习方法总结--摘录篇
·J2ME程序开发新手入门九大要点
·J2ME综合:如何在MIDP中实现图片放缩
·J2ME与WebService-KSOAP快速上手
·如何使用“月蚀”进行J2ME开发
·J2ME概述
·J2ME进度条与线程化模型实例解析
·J2ME实现可伸展目录树TreeList(图)
·J2ME的现状与发展
·告诉你J2ME是什么
·知己知彼——J2ME技术详
·Java思路开发易于移植的J2ME游戏
·关于J2ME——MIDP1.0中的碰撞检测
·J2ME工具:使用j2meunit进行游戏测试
·在J2ME中读取各种格式的文本文件
·如何搭建J2ME的开发环境之一二
·J2ME学习系列之如何将J2ME与XML集成
·使用J2ME中的page进行编码转化

    您现在的位置: Linux宝库 >> Java >> J2ME >> 核心技术 >> 文章正文
熟练使用J2ME在实际开发中的可选包MMAPI
Linux宝库 收集整理  作者:Linux宝库  时间:2007-12-31  收藏本站
来自:http://doc.linuxpk.com/45923.html
联系:linuxmine#gmail.com
分类:[核心技术]
  本文的目的是为读者提供处理不同情况的代码,您可以参考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)

  {

  }
本文来自:http://doc.linuxpk.com/45923.html
 
     最新更新
·深入了解J2ME的几个重要概念
·J2ME学习笔记(6)—连接MIDlet到文本文件
·J2ME学习笔记(5)—MIDlets中的图形编程
·J2ME学习笔记(4)—用MIDPAPI开发MIDlets
·J2ME学习笔记(3)—初次接触MIDlets
·J2ME学习笔记(2)—平台体系结构详解
·J2ME学习笔记(1)—平台介绍及简单实例
·J2METimer-更简单的实现多任务调度执行
·J2ME基础知识
·J2MEMIDP提供的最重要的图形元素
·精通J2ME中的HelloWorld
·J2METimer使用指南
·请关注J2MEWTK2.2的新特性
·详细介绍并掌握J2ME的安全结构
·如何使用“月蚀”进行J2ME开发
·如何搭建J2ME的开发环境之一二
·J2ME编程实例---之数字键的测试
·利用J2ME与ASP建立数据库连接
·J2ME平台构件及开发应用程序示例
·J2ME(CLDC/MIDP)简介
·关于J2MEMIDP1.0.3中的安全问题简述
·学习在J2ME中使用代码
·J2ME的起源和发展历程
·认识了解“J2ME”
·探索J2ME:用GCF通信
·探索J2ME:使用记录管理系统
·探索J2ME:对记录进行排序
·浅析J2EE、J2SE和J2ME
·J2ME应用基础
·探索J2ME:创建开销细节表单