首 页 | 新 闻 | Symbian | Windows Mobile| J2ME | 下载中心 | 游戏策划 | 购书指南 | 移动开发视频教程
您现在的位置: 开发视界 >> J2ME >> 可选包 >> 文章正文
mmapi 在j2me polish中的应用
作者:佚名    文章来源:JavaEye.com    更新时间:2007-11-5 15:13:49

近日工作中,要在j2me polish项目里实现mmapi, 其中的视频的实现在j2me polish与一般j2me项目里是不同的,弄了一天都不能播放
。后来在网上问了一个高手才知道,下面我们看看Video Play 在polish中的实现:

j2me里实现mmapi的基本解释我不多说了,不了解的可以参考 http://hi.baidu.com/cobalt/blog/item/fdb457c25e4c0237e4dd3b71.html 里边讲得很详细的,我们主要看看实现mmapi video的播放的代码:

java 代码:

代码
  1. try {   
  2.     InputStream is = getClass().getResourceAsStream("/3.mpg");   
  3.     Player p = Manager.createPlayer(is, "video/mpeg");   
  4.     p.realize();   
  5.     // Grab the video control and set it to the current display.   
  6.     VideoControl vc = (VideoControl)p.getControl("VideoControl");   
  7.     if (vc != null) {   
  8.         Form form = new Form("Video form");   
  9.         form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));   
  10.         display.setCurrent(form);   
  11.     }   
  12.     p.start();   
  13. catch (IOException ioe) {   
  14. catch (MediaException me) { }   
  15.   

当我在j2me polish工程中按这样实现的时候,老是出现下边的异常:

代码
  1. Generic/DefaultColorPhone: startApp threw an Exception   
  2. Generic/DefaultColorPhone: java.lang.ClassCastException   
  3. Generic/DefaultColorPhone: java.lang.ClassCastException   
  4. Generic/DefaultColorPhone:  at com.protel.MM.UI.MMMidlet.startApp(+224)   
  5. Generic/DefaultColorPhone: [javac] C:\Documents and Settings\winxp\Desktop\mmapi\source\src\com\protel\MM\UI\MMMidlet.java:57: form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));   
  6. Generic/DefaultColorPhone:  at javax.microedition.midlet.MIDletProxy.startApp(+7)   
  7. Generic/DefaultColorPhone:  at com.sun.midp.midlet.Scheduler.schedule(+270)   
  8. Generic/DefaultColorPhone:  at com.sun.midp.main.Main.runLocalClass(+28)   
  9. Generic/DefaultColorPhone:  at com.sun.midp.main.Main.main(+116)   

原来在j2me polish应用中所有的Screen或Item都经过preproceses(预处理),故所有新创建或返回的Screen/Item都默认是de.enough.polish.ui.*里边的,
所以直接调用上边的代码时会发生类型转换异常。所以要在j2me polish项目中正常播放video要直接引用javax.microedition.lcdui.*
的类,修改后的代码如下:

代码
  1. try { ~~~~~~~~~~"+getClass().getResourceAsStream("/3.mpg"));  
  2.     InputStream is = getClass().getResourceAsStream("/3.mpg");  
  3.     Player p = Manager.createPlayer(is, "video/mpeg");  
  4.     p.realize();  
  5.     // Grab the video control and set it to the current display.  
  6.     VideoControl vc = (VideoControl)p.getControl("VideoControl");  
  7.     if (vc != null) {  
  8.         javax.microedition.lcdui.Form form = new javax.microedition.lcdui.Form("Video form");   
  9.         javax.microedition.lcdui.Item videoItem = (javax.microedition.lcdui.Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);   
  10.         form.append(videoItem);   
  11.            
  12.         display.setCurrent(form);   
  13.     }   
  14.     p.start();   
  15. catch (IOException ioe) {   
  16. catch (MediaException me) { }   
相关文章:
用MMAPI拍照