冰浪

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

          使用MMAPI播放gif動畫

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

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

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

          首先我們應該準備一個gif文件,放在能夠訪問到的位置,且確保這個文件會被打包進jar內(nèi)。在創(chuàng)建播放器之前應該先確認手機上的MMAPI實現(xiàn)是否支持image/gif格式的播放,如果支持則創(chuàng)建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創(chuàng)建后,我們需要獲得VideoControl,然后將內(nèi)容渲染到屏幕上,VideoControl提供了兩種模式,這里我們使用USE_GUI_PRIMITIVE方式,將返回的Item追加到一個Form中顯示。最后調(diào)用Player.start()即可播放。注意在退出之前一定要釋放Player資源,關(guān)閉Player并設(shè)置為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. }  
          (轉(zhuǎn)于http://azi.javaeye.com/blog/177126)
          主站蜘蛛池模板: 施秉县| 文登市| 郴州市| 德令哈市| 齐齐哈尔市| 迭部县| 马鞍山市| 尤溪县| 神木县| 通河县| 手机| 西华县| 望都县| 宝丰县| 余姚市| 峡江县| 中江县| 无为县| 海宁市| 全椒县| 达拉特旗| 大荔县| 长沙市| 台安县| 边坝县| 康马县| 兰考县| 巴彦淖尔市| 韶山市| 澄城县| 湖南省| 阿克苏市| 叙永县| 张家口市| 都安| 铜鼓县| 日照市| 安阳市| 安陆市| 襄城县| 白朗县|