比如給百寶箱做游戲
下面提供一種解決方案
關鍵問題
解決方案
// Player types
static final int STANDARD =
static final int NOKIA =
static final int SAMSUNG =
static final int NEC =
static final String[] supportedPlayerTypes = {
};
下面利用反射機制來動態加載
public void determinePlayerType() {
// use most
isSupportSound = true;
for (int i =
// try to load a proper sound Player
try {
Class
playerType = i; //保存加載成功的類的類型
return;
} catch (Exception e) { //加載不成功
e
}
}
isSupportSound = false;
}
public void createPlayer(String name) {
if (!isSupportSound)
return;
switch (playerType) {
case STANDARD: // for MIDI
case NEC:
createPlayerFactory(
break;
case NOKIA: //for ott
createPlayerFactory(
break;
case SAMSUNG: // for mmf
createPlayerFactory(
break;
}
}
該方案優點
注意
遺留問題
測試機型
下面是源程序
/**
* Created on
* This class is mainly for the games on various mobile platform
* If U have any good ideas about the J
*
* Version
*/
import javax
import java
//********************* STANDARD ***********************
import jadia
//********************* NOKIA ***********************
import com
//********************* SAMSUNG ***********************
import com
// ********************* NEC ***********************
import dia
//********************* SonyEricsson ***********************
// the same as J
/**
* @author IntoTheDream
*/
public class MyPlayer {
private static MyPlayer mp = null;
//Sound types
static final int MIDI =
static final int MMF =
static final int OTT =
// Player types
static final int STANDARD =
static final int NOKIA =
static final int SAMSUNG =
static final int NEC =
static final String[] supportedSoundTypes = {
private static int soundType;
static final String[] supportedPlayerTypes = {
private static int playerType;
// Note : first
// Of course you can change it in the game
static boolean isSupportSound;
static boolean isSoundOn = true;
// ********************* STANDARD ***********************
Player player;
// ********************* NOKIA ***********************
Sound nokiaPlayer;
// ********************* SAMSUNG ***********************
com
// ********************* NEC ***********************
dia
// singleton
private MyPlayer() {
}
public static MyPlayer getMyPlayer() {
if (mp == null) {
mp = new MyPlayer();
mp
}
return mp;
}
// automatically run this first !!
public void determinePlayerType() {
// use most
isSupportSound = true;
for (int i =
// try to load a proper sound Player
try {
Class
playerType = i;
return;
} catch (Exception e) {
e
}
}
isSupportSound = false;
}
public void createPlayer(String name) {
if (!isSupportSound)
return;
switch (playerType) {
case STANDARD: // for MIDI
case NEC:
createPlayerFactory(
break;
case NOKIA: //for ott
createPlayerFactory(
break;
case SAMSUNG: // for mmf
createPlayerFactory(
break;
}
}
private void createPlayerFactory(String url) {
if (isSupportSound && isSoundOn) {
try {
InputStream is;
switch (playerType) {
case NEC:
necPlayer = Media
break;
case STANDARD: //must be dimissed for NECN
is = this
player = Manager
break;
case NOKIA:
is = this
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int b = is
baos
}
nokiaPlayer = new com
com
break;
case SAMSUNG:
samsungPlayer = new com
com
break;
}
} catch (Exception e) {
e
}
}
}
public void play(int loop) {
if (isSupportSound && isSoundOn) {
try {
switch (playerType) {
case NEC:
if (necPlayer == null)
break;
necPlayer
necPlayer
break;
case STANDARD: //must be dimissed for NECN
if (player == null)
break;
player
player
break;
case NOKIA:
if (nokiaPlayer == null)
break;
nokiaPlayer
break;
case SAMSUNG:
if (samsungPlayer == null)
break;
samsungPlayer
break;
}
} catch (Exception e) {
e
}
}
}
public void stopSound() {
if (isSupportSound && isSoundOn) {
try {
switch (playerType) {
case STANDARD: //must be dimissed for NECN
if (player == null)
break;
player
player
player
player = null;
break;
case NOKIA:
if (nokiaPlayer == null)
break;
nokiaPlayer
nokiaPlayer = null;
break;
case SAMSUNG:
if (samsungPlayer == null)
break;
samsungPlayer
samsungPlayer = null;
break;
case NEC:
if (necPlayer == null)
break;
necPlayer
necPlayer = null;
break;
}
System
} catch (Exception e) {
e
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26841.html