歡迎指教, [email protected]
"Write once,run anywhere" 是Java的口號,但在J2ME平台上做的利用,要想不修正代碼就run anywhere,難度是很大的。假如要把一個利用程序做到讓大多數的機型都實用,就要考慮到方方面面,其難度是相當大的。
比如給百寶箱做游戲,上線機型大多是MIDP1。0的機器,感到移植中最麻煩的還要數聲音部分的API,必需根據各個機型來修正。固然圖象還比擬輕易做成自適應的,但聲音部分就一般就只能根據各個機型來修正。
下面供給一種解決計劃,可以讓J2ME程序在運行時主動加載該機型支撐的聲音資源並用該
機型的聲音API來播放。
要害標題: 1。各機型供給的播放音樂的API都有所不同,特別是較老的機型。
需要在運行時根據機型主動加載。
2。各機型支撐的聲音的資源文件也不同。需要在運行時根據機型主動加載。
3。各機型的JVM不同,多多少少有一些比擬特別的BUG。
解決計劃: 1。原則:能用尺度API就用尺度API,不能用的話,就用各個機型自身的API。
// Player types
static final int STANDARD = 0; //For MIDI
static final int NOKIA = 1; //For ott
static final int SAMSUNG = 2; //For mmf
static final int NEC = 3; //For MIDI
static final String[] supportedPlayerTypes = {
"Javax.microedition.media.Player", //STANDARD API
"com.nokia.mid.sound.Sound", // 諾基亞
"com.samsung.util.AudioClip", //samsung
"com.nec.media.AudioClip", //nec
};
下面利用反射機制來動態加載:
public void determinePlayerType() {
// use most -> less use
isSupportSound = true;
for (int i = 0; i < supportedPlayerTypes.length; i++) {
// try to load a proper sound Player
try {
Class.forName(supportedPlayerTypes[i]); //加載當前的Player類型
playerType = i; //保留加載成功的類的類型
return;
} catch (Exception e) { //加載不成功,闡明不支撐,持續加載下一種
e.printStackTrace();
}
}
isSupportSound = false;
}
2。下面就可以根據在載成功的類型來加載可以播放的聲音資源了
public void createPlayer(String name) {
if (!isSupportSound)
return;
switch (playerType) {
case STANDARD: // for MIDI
case NEC:
createPlayerFactory("/" + name + ".mid");
break;
case NOKIA: //for ott
createPlayerFactory("/" + name + ".ott");
break;
case SAMSUNG: // for mmf
createPlayerFactory("/" + name + ".mmf");
break;
}
}
3。對各個機型特有的BUG,是沒有什麼特別好的措施的,只能各個機型調試。這只能怪廠商了。。。。。
該計劃長處:在移植的時候就不用修正代碼。只要在相應的機型JAR包中保留相干的資源就可以了。這樣就不用為了各個機型都折騰一遍了。
留心 :用 System.getProperty("microedition.platform")來斷定機型是不保險的,由於有的機型只是簡略地返回J2ME platform。
遺留標題:
1NecN820 在運行Class.forName("javax.microedition.media.Player");時候會立即報“利用程序出錯”,而不是拋出“ClassNotFoundException”異常。這是該機型JVM的特征(BUG),所以給NecN820的代碼中必需注釋掉Javax.microedition.media.Player的一切信息。這就得修正代碼,有違我們的初衷,的確是個遺憾。(估計NEC的機型都素這樣的)
2 這個類還有待擴大,以支撐更多機型。並加進震動部分的API。理論上可以包含所有的機型。但實際利用中只要包含需要用到的機型相干API就可以了。
測試機型: 在 三星E708,MOTOV600,NOKIA 7650 ,NecN820(注釋掉Javax.microedition.media.Player相干內容)上均測試通過。
下面是源程序:
/**
* Created on 2005-7-4
* This class is mainly for the games on various mobile platform.
* If U have any good ideas about the J2ME, contact me at [email protected]
*
* Version 1.0
*/
import Javax.microedition.lcdui.*;
import Java.io.*;
//********************* STANDARD ***********************
import Javax.microedition.media.*;
//********************* NOKIA ***********************
import com.nokia.mid.sound.*;
//********************* SAMSUNG ***********************
import com.samsung.util.*;
// ********************* NEC ***********************
import com.nec.media.*;
//********************* SonyEriCSSon ***********************
// the same as J2ME standard API
/**
* @author IntoTheDream
*/
public class MyPlayer {
private static MyPlayer mp = null;
//Sound types, to be enlarged....
static final int MIDI = 0;
static final int MMF = 1;
static final int OTT = 2;
// Player types
static final int STANDARD = 0; //For MIDI
static final int NOKIA = 1; //For ott
static final int SAMSUNG = 2; //For mmf
static final int NEC = 3; //For MIDI
static final String[] supportedSoundTypes = { "mid", "mmf", "ott", };
private static int soundType;
static final String[] supportedPlayerTypes = {
"Javax.microedition.media.Player", //must be dimissed for NECN820
"com.nokia.mid.sound.Sound", "com.samsung.util.AudioClip",
"com.nec.media.AudioClip", };
private static int playerType;
// Note : first , play sound to determine whether sound On or Off
// Of course you can change it in the game
static boolean isSupportSound;
static boolean isSoundOn = true;
// ********************* STANDARD ***********************
Player player;
// ********************* NOKIA ***********************
Sound nokiaPlayer;
// ********************* SAMSUNG ***********************
com.samsung.util.AudioClip samsungPlayer;
// ********************* NEC ***********************
com.nec.media.AudioClip necPlayer;
// singleton
private MyPlayer() {
}
public static MyPlayer getMyPlayer() {
if (mp == null) {
mp = new MyPlayer();
mp.determinePlayerType();
}
return mp;
}
// automatically run this first !!
public void determinePlayerType() {
// use most -> less use
isSupportSound = true;
for (int i = 0; i < supportedPlayerTypes.length; i++) {
// try to load a proper sound Player
try {
Class.forName(supportedPlayerTypes[i]);
playerType = i;
return;
} catch (Exception e) {
e.printStackTrace();
}
}
isSupportSound = false;
}
public void createPlayer(String name) {
if (!isSupportSound)
return;
switch (playerType) {
case STANDARD: // for MIDI
case NEC:
createPlayerFactory("/" + name + ".mid");
break;
case NOKIA: //for ott
createPlayerFactory("/" + name + ".ott");
break;
case SAMSUNG: // for mmf
createPlayerFactory("/" + name + ".mmf");
break;
}
}
private void createPlayerFactory(String url) {
if (isSupportSound && isSoundOn) {
try {
InputStream is;
switch (playerType) {
case NEC:
necPlayer = Media.getAudioClip(url);
break;
case STANDARD: //must be dimissed for NECN820
is = this.getClass().getResourceAsStream(url);
player = Manager.createPlayer(is, "audio/midi");
break;
case NOKIA:
is = this.getClass().getResourceAsStream(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int b = is.read(); b >= 0; b = is.read()) {
baos.write(b);
}
nokiaPlayer = new com.nokia.mid.sound.Sound(baos
.toByteArray(),
com.nokia.mid.sound.Sound.FORMAT_TONE);
break;
case SAMSUNG:
samsungPlayer = new com.samsung.util.AudioClip(
com.samsung.util.AudioClip.TYPE_MMF, url);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void play(int loop) {
if (isSupportSound && isSoundOn) {
try {
switch (playerType) {
case NEC:
if (necPlayer == null)
break;
necPlayer.setLoopCount(loop);
necPlayer.play();
break;
case STANDARD: //must be dimissed for NECN820
if (player == null)
break;
player.setLoopCount(loop);
player.start();
break;
case NOKIA:
if (nokiaPlayer == null)
break;
nokiaPlayer.play(loop);
break;
case SAMSUNG:
if (samsungPlayer == null)
break;
samsungPlayer.play(loop, 5); // 5 is volume
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stopSound() {
if (isSupportSound && isSoundOn) {
try {
switch (playerType) {
case STANDARD: //must be dimissed for NECN820
if (player == null)
break;
player.deallocate();
player.stop();
player.close();
player = null;
break;
case NOKIA:
if (nokiaPlayer == null)
break;
nokiaPlayer.stop();
nokiaPlayer = null;
break;
case SAMSUNG:
if (samsungPlayer == null)
break;
samsungPlayer.stop();
samsungPlayer = null;
break;
case NEC:
if (necPlayer == null)
break;
necPlayer.stop();
necPlayer = null;
break;
}
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
另: 關於程序的編譯, 可以把各個機型的API做為LIB加進eclipse工程。