要在J2ME polish項目裡實現mmapi, 其中的視頻的實現在j2me polish與一般J2ME項目裡是不同的,弄了一天都不能播放
。後來在網上問了一個高手才知道,下面我們看看Video Play 在polish中的實現:
J2ME裡實現mmapi的基本解釋我不多說了,不了解的可以參考 http://hi.baidu.com/cobalt/blog/item/fdb457c25e4c0237e4dd3b71.Html 裡邊講得很詳細的,我們主要看看實現mmapi video的播放的代碼:
Java 代碼:
代碼
- try {
- InputStream is = getClass().getResourceAsStream("/3.mpg");
- Player p = Manager.createPlayer(is, "video/mpeg");
- p.realize();
- // Grab the video control and set it to the current display.
- VideoControl vc = (VideoControl)p.getControl("VideoControl");
- if (vc != null) {
- Form form = new Form("Video form");
- form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));
- display.setCurrent(form);
- }
- p.start();
- } catch (IOException ioe) {
- } catch (MediaException me) { }
-
當我在J2ME polish工程中按這樣實現的時候,老是出現下邊的異常:
代碼
- Generic/DefaultColorPhone: startApp threw an Exception
- Generic/DefaultColorPhone: Java.lang.ClassCastException
- Generic/DefaultColorPhone: Java.lang.ClassCastException
- Generic/DefaultColorPhone: at com.protel.MM.UI.MMMidlet.startApp(+224)
- 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));
- Generic/DefaultColorPhone: at Javax.microedition.midlet.MIDletProxy.startApp(+7)
- Generic/DefaultColorPhone: at com.sun.midp.midlet.Scheduler.schedule(+270)
- Generic/DefaultColorPhone: at com.sun.midp.main.Main.runLocalClass(+28)
- 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.*
的類,修改後的代碼如下:
代碼
- try { ~~~~~~~~~~"+getClass().getResourceAsStream("/3.mpg"));
- InputStream is = getClass().getResourceAsStream("/3.mpg");
- Player p = Manager.createPlayer(is, "video/mpeg");
- p.realize();
- // Grab the video control and set it to the current display.
- VideoControl vc = (VideoControl)p.getControl("VideoControl");
- if (vc != null) {
- javax.microedition.lcdui.Form form = new Javax.microedition.lcdui.Form("Video form");
- javax.microedition.lcdui.Item videoItem = (Javax.microedition.lcdui.Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
- form.append(videoItem);
-
- display.setCurrent(form);
- }
- p.start();
- } catch (IOException ioe) {
- } catch (MediaException me) { }