服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

熟练使用j2me在实际开发中的可选包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)
  {
  }

扫描关注微信公众号