Posted on 2009-04-09 09:42
冰浪 閱讀(217)
評論(0) 編輯 收藏 所屬分類:
J2ME
MMAPI是在JSR 135中提出的,增強了Java ME平臺對多媒體編程的支持。例如播放音頻和視頻文件,捕獲聲音和圖像等。目前大多數(shù)支持JTWI 1.0的手機都支持了MMAPI。本文介紹如何使用MMAPI播放gif格式的動畫。
其實制作動畫效果可以有很多辦法,例如準備一個圖片數(shù)組,在程序中不斷的切換顯示不同的圖片,只要時間的間隔設置合理即可出現(xiàn)動畫的效果。如果使用MMAPI則可以直接播放gif的動畫。其實這和播放視頻文件的方法和流程是類似的。
首先我們應該準備一個gif文件,放在能夠訪問到的位置,且確保這個文件會被打包進jar內(nèi)。在創(chuàng)建播放器之前應該先確認手機上的MMAPI實現(xiàn)是否支持image/gif格式的播放,如果支持則創(chuàng)建Player,如下。
- private void createPlayer(InputStream is){
- String[] contents = Manager.getSupportedContentTypes(null);
- for(int i = 0;i<contents.length;i++){
- if(contents[i].toLowerCase().indexOf("image/gif") != -1){
- try {
- player = Manager.createPlayer(is,"image/gif");
- player.realize();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (MediaException ex) {
- ex.printStackTrace();
- }
-
- }
-
- }
- }
private void createPlayer(InputStream is){
String[] contents = Manager.getSupportedContentTypes(null);
for(int i = 0;i<contents.length;i++){
if(contents[i].toLowerCase().indexOf("image/gif") != -1){
try {
player = Manager.createPlayer(is,"image/gif");
player.realize();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
}
Player創(chuàng)建后,我們需要獲得VideoControl,然后將內(nèi)容渲染到屏幕上,VideoControl提供了兩種模式,這里我們使用USE_GUI_PRIMITIVE方式,將返回的Item追加到一個Form中顯示。最后調(diào)用Player.start()即可播放。注意在退出之前一定要釋放Player資源,關閉Player并設置為null。GifPlayer的源碼如下:
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.Item;
- import javax.microedition.media.Manager;
- import javax.microedition.media.MediaException;
- import javax.microedition.media.Player;
- import javax.microedition.media.control.VideoControl;
- import javax.microedition.midlet.*;
-
-
- public class GifPlayer extends MIDlet {
-
- private Display display = null;
- private Player player = null;
-
- public void startApp() {
- if(display == null)
- display = Display.getDisplay(this);
- Form form = new Form("gif player");
- InputStream is = getClass().getResourceAsStream("/a.gif");
- createPlayer(is);
- if(player == null){
- form.append("can not play image/gif");
- }else{
- VideoControl vc = (VideoControl)player.getControl("VideoControl");
- if(vc != null){
- form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null));
- }
- }
- display.setCurrent(form);
- try {
- player.start();
- } catch (MediaException ex) {
- ex.printStackTrace();
- }
- }
-
- private void createPlayer(InputStream is){
- String[] contents = Manager.getSupportedContentTypes(null);
- for(int i = 0;i<contents.length;i++){
- if(contents[i].toLowerCase().indexOf("image/gif") != -1){
- try {
- player = Manager.createPlayer(is,"image/gif");
- player.realize();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (MediaException ex) {
- ex.printStackTrace();
- }
-
- }
-
- }
- }
-
- public void pauseApp() {
- }
-
- public void destroyApp(boolean unconditional) {
- if(player != null){
- player.close();
- player = null;
- }
- }
- }
(轉(zhuǎn)于http://azi.javaeye.com/blog/177126)