熟练使用J2ME在实际开发中的可选包MMAPI

2016-02-19 13:36 4 1 收藏

下面图老师小编跟大家分享熟练使用J2ME在实际开发中的可选包MMAPI,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

【 tulaoshi.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://www.tulaoshi.com/n/20160219/1604731.html

延伸阅读
概述 目前,很多手机已经具备了蓝牙功能。虽然MIDP2.0没有包括蓝牙API,但是JCP定义了JSR82, Java APIs for Bluetooth Wireless Technology (JABWT).这是一个可选API,很多支持MIDP2.0的手机已经实现了,比如Nokia 6600, Nokia 6670,Nokia7610等等。对于一个开发者来说,假如目标平台支...
在J2ME中,处理声音需要使用到Mobile Media API(MMAPI),该包是MIDP1.0的可选包,在MIDP2.0中已经包含了这个包。所以假如你使用MIDP1.0的话,请确认你的运行环境是否支持。 一般手机支持的声音文件格式为wav、mid和mpg等。具体请查阅你的手机说明文档。 !-- frame contents -- !-- /frame contents -- 在声音处...
一.代码优化 内存会溢出肯定和代码逃不了关系,99.99%学java的人都知道垃圾回收器是java的一大优点并据此来嘲笑C++。显然这个特性为代码编写者省了不少事,但这个特性却带来了不少隐患。举个例子在游戏当中经常有不同场景的切换,如从游戏逻辑退到主菜单逻辑,对游戏逻辑对象的态度很多人会选择忘记等待垃圾回收器来收尸。乍看之下似乎并无不...
二、开发 J2ME应用程序 在本节中,我们将介绍使用 EclipseMe 插件构建 J2ME 应用程序的基本步骤。首先,您将创建一个 J2ME 项目。然后,您可以开始您的J2ME开发之旅。 1 创建 J2ME项目 要创建一个 J2ME 项目,回到"File"菜单,选择"New"项,并在子菜单中选择"Project"。然后执行以下步骤: ...
在GCF中提供了DatagramConnection和Datagram两个接口,借助他们我们可以在J2ME中基于UDP协议开发联网应用程序,在MIDP2.0中,添加了UDPDatagramConnection这个接口。他扩展了DatagramConnection并添加了两个方法getLocalAddress()和getLocalPort()。我们知道UDP服务是不可靠的,假如你希望开发更可靠的联网应用的话可以采用SocketConnection,因...

经验教程

450

收藏

32
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部