冰浪

          哥已不再年輕 - 堅定夢想,畢生追求!
          posts - 85, comments - 90, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          使用MMAPI播放gif動畫

          Posted on 2009-04-09 09:42 冰浪 閱讀(219) 評論(0)  編輯  收藏 所屬分類: J2ME

          MMAPI是在JSR 135中提出的,增強了Java ME平臺對多媒體編程的支持。例如播放音頻和視頻文件,捕獲聲音和圖像等。目前大多數支持JTWI 1.0的手機都支持了MMAPI。本文介紹如何使用MMAPI播放gif格式的動畫。

          其實制作動畫效果可以有很多辦法,例如準備一個圖片數組,在程序中不斷的切換顯示不同的圖片,只要時間的間隔設置合理即可出現動畫的效果。如果使用MMAPI則可以直接播放gif的動畫。其實這和播放視頻文件的方法和流程是類似的。

          首先我們應該準備一個gif文件,放在能夠訪問到的位置,且確保這個文件會被打包進jar內。在創建播放器之前應該先確認手機上的MMAPI實現是否支持image/gif格式的播放,如果支持則創建Player,如下。

           

          Java代碼 復制代碼
          1. private void createPlayer(InputStream is){   
          2.        String[] contents = Manager.getSupportedContentTypes(null);   
          3.        for(int i = 0;i<contents.length;i++){   
          4.            if(contents[i].toLowerCase().indexOf("image/gif") != -1){   
          5.                try {   
          6.                    player = Manager.createPlayer(is,"image/gif");   
          7.                    player.realize();   
          8.                } catch (IOException ex) {   
          9.                    ex.printStackTrace();   
          10.                } catch (MediaException ex) {   
          11.                    ex.printStackTrace();   
          12.                }   
          13.                   
          14.            }   
          15.               
          16.        }   
          17.    }  

           

          Player創建后,我們需要獲得VideoControl,然后將內容渲染到屏幕上,VideoControl提供了兩種模式,這里我們使用USE_GUI_PRIMITIVE方式,將返回的Item追加到一個Form中顯示。最后調用Player.start()即可播放。注意在退出之前一定要釋放Player資源,關閉Player并設置為null。GifPlayer的源碼如下:

           

          Java代碼 復制代碼
          1. import java.io.IOException;   
          2. import java.io.InputStream;   
          3. import javax.microedition.lcdui.Display;   
          4. import javax.microedition.lcdui.Form;   
          5. import javax.microedition.lcdui.Item;   
          6. import javax.microedition.media.Manager;   
          7. import javax.microedition.media.MediaException;   
          8. import javax.microedition.media.Player;   
          9. import javax.microedition.media.control.VideoControl;   
          10. import javax.microedition.midlet.*;   
          11.   
          12.   
          13. public class GifPlayer extends MIDlet {   
          14.        
          15.     private Display display = null;   
          16.     private Player player = null;   
          17.        
          18.     public void startApp() {   
          19.         if(display == null)   
          20.             display = Display.getDisplay(this);   
          21.         Form form = new Form("gif player");   
          22.         InputStream is = getClass().getResourceAsStream("/a.gif");   
          23.         createPlayer(is);   
          24.         if(player == null){   
          25.             form.append("can not play image/gif");   
          26.         }else{   
          27.             VideoControl vc = (VideoControl)player.getControl("VideoControl");   
          28.             if(vc != null){   
          29.                 form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null));   
          30.             }   
          31.         }   
          32.         display.setCurrent(form);   
          33.         try {   
          34.             player.start();   
          35.         } catch (MediaException ex) {   
          36.             ex.printStackTrace();   
          37.         }   
          38.     }   
          39.        
          40.     private void createPlayer(InputStream is){   
          41.         String[] contents = Manager.getSupportedContentTypes(null);   
          42.         for(int i = 0;i<contents.length;i++){   
          43.             if(contents[i].toLowerCase().indexOf("image/gif") != -1){   
          44.                 try {   
          45.                     player = Manager.createPlayer(is,"image/gif");   
          46.                     player.realize();   
          47.                 } catch (IOException ex) {   
          48.                     ex.printStackTrace();   
          49.                 } catch (MediaException ex) {   
          50.                     ex.printStackTrace();   
          51.                 }   
          52.                    
          53.             }   
          54.                
          55.         }   
          56.     }   
          57.        
          58.     public void pauseApp() {   
          59.     }   
          60.        
          61.     public void destroyApp(boolean unconditional) {   
          62.         if(player != null){   
          63.             player.close();   
          64.             player = null;   
          65.         }   
          66.     }   
          67. }  
          (轉于http://azi.javaeye.com/blog/177126)
          主站蜘蛛池模板: 太仆寺旗| 沿河| 冀州市| 宁城县| 遂宁市| 尼勒克县| 罗平县| 贵州省| 昌江| 都江堰市| 雷波县| 丽水市| 中宁县| 马山县| 汝州市| 石家庄市| 三门峡市| 侯马市| 乐昌市| 天柱县| 邮箱| 长兴县| 怀化市| 永寿县| 体育| 牙克石市| 苏尼特右旗| 盈江县| 宝鸡市| 黔江区| 崇左市| 龙门县| 吉水县| 宁国市| 孝义市| 江达县| 临沭县| 乌兰浩特市| 敖汉旗| 兴义市| 华安县|