java

位置:IT落伍者 >> java >> 浏览文章

JavaME程序 Run Anywhere-- 利用反射机制来动态加载声


发布日期:2018年01月01日
 
JavaME程序 Run Anywhere-- 利用反射机制来动态加载声
让JavaME程序 Run Anywhere 利用反射机制来动态加载声音API 欢迎指教 tcn Write oncerun anywhere 是JAVA的口号但在JME平台上做的应用要想不改动代码就run anywhere难度是很大的如果要把一个应用程序做到让大多数的机型都适用就让JavaME程序 Run Anywhere

利用反射机制来动态加载声音API

Write oncerun anywhere 是JAVA的口号但在JME平台上做的应用要想不改动代码就run anywhere难度是很大的如果要把一个应用程序做到让大多数的机型都适用就要考虑到方方面面其难度是相当大的

比如给百宝箱做游戏上线机型大多是MIDP的机器感觉移植中最麻烦的还要数声音部分的API必须根据各个机型来改动虽然图象还比较容易做成自适应的但声音部分就一般就只能根据各个机型来改动

下面提供一种解决方案可以让JME程序在运行时自动加载该机型支持的声音资源并用该机型的声音API来播放

关键问题 各机型提供的播放音乐的API都有所不同特别是较老的机型需要在运行时根据机型自动加载

各机型支持的声音的资源文件也不同需要在运行时根据机型自动加载

各机型的JVM不同多多少少有一些比较特别的BUG

解决方案 原则能用标准API就用标准API不能用的话就用各个机型自身的API

// Player types

static final int STANDARD = ; //For MIDI

static final int NOKIA = ; //For ott

static final int SAMSUNG = ; //For mmf

static final int NEC = ; //For MIDI

static final String[] supportedPlayerTypes = {

jadiaPlayer //STANDARD API

comnokiamidsoundSound // Nokia

comsamsungutilAudioClip //samsung

diaAudioClip //nec

};

下面利用反射机制来动态加载

public void determinePlayerType() {

// use most > less use

isSupportSound = true;

for (int i = ; i < supportedPlayerTypeslength; i ) {

// try to load a proper sound Player

try {

ClassforName(supportedPlayerTypes[i]); //加载当前的Player类型

playerType = i; //保存加载成功的类的类型

return;

} catch (Exception e) { //加载不成功说明不支持继续加载下一种

eprintStackTrace();

}

}

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;

}

}

对各个机型特有的BUG是没有什么特别好的办法的只能各个机型调试这只能怪厂商了

该方案优点在移植的时候就不用改动代码只要在相应的机型JAR包中保留相关的资源就可以了这样就不用为了各个机型都折腾一遍了

注意 用 SystemgetProperty(microeditionplatform)来确定机型是不保险的因为有的机型只是简单地返回JME platform

遗留问题

NecN 在运行 ClassforName(jadiaPlayer);时候会立刻报应用程序出错而不是抛出ClassNotFoundException异常这是该机型JVM的特性(BUG)所以给NecN的代码中必须注释掉jadiaPlayer的一切信息这就得改动代码有违我们的初衷的确是个遗憾(估计NEC的机型都素这样的)

这个类还有待扩展以支持更多机型并加入震动部分的API理论上可以包含所有的机型但实际应用中只要包含需要用到的机型相关API就可以了

测试机型 在 三星EMOTOVNOKIA NecN(注释掉jadiaPlayer相关内容)上均测试通过

下面是源程序

/**

* Created on

* This class is mainly for the games on various mobile platform

* If U have any good ideas about the JME contact me at tcn

*

* Version

*/

import javaxmicroeditionlcdui*;

import javaio*;

//********************* STANDARD ***********************

import jadia*;

//********************* NOKIA ***********************

import comnokiamidsound*;

//********************* SAMSUNG ***********************

import comsamsungutil*;

// ********************* NEC ***********************

import dia*;

//********************* SonyEricsson ***********************

// the same as JME standard API

/**

* @author IntoTheDream

*/

public class MyPlayer {

private static MyPlayer mp = null;

//Sound types to be enlarged

static final int MIDI = ;

static final int MMF = ;

static final int OTT = ;

// Player types

static final int STANDARD = ; //For MIDI

static final int NOKIA = ; //For ott

static final int SAMSUNG = ; //For mmf

static final int NEC = ; //For MIDI

static final String[] supportedSoundTypes = { mid mmf ott };

private static int soundType;

static final String[] supportedPlayerTypes = {

jadiaPlayer //must be dimissed for NECN

comnokiamidsoundSound comsamsungutilAudioClip

diaAudioClip };

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 ***********************

comsamsungutilAudioClip samsungPlayer;

// ********************* NEC ***********************

diaAudioClip necPlayer;

// singleton

private MyPlayer() {

}

public static MyPlayer getMyPlayer() {

if (mp == null) {

mp = new MyPlayer();

mpdeterminePlayerType();

}

return mp;

}

// automatically run this first !!

public void determinePlayerType() {

// use most > less use

isSupportSound = true;

for (int i = ; i < supportedPlayerTypeslength; i ) {

// try to load a proper sound Player

try {

ClassforName(supportedPlayerTypes[i]);

playerType = i;

return;

} catch (Exception e) {

eprintStackTrace();

}

}

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 = MediagetAudioClip(url);

break;

case STANDARD: //must be dimissed for NECN

is = thisgetClass()getResourceAsStream(url);

player = ManagercreatePlayer(is audio/midi);

break;

case NOKIA:

is = thisgetClass()getResourceAsStream(url);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

for (int b = isread(); b >= ; b = isread()) {

baoswrite(b);

}

nokiaPlayer = new comnokiamidsoundSound(baos

toByteArray()

comnokiamidsoundSoundFORMAT_TONE);

break;

case SAMSUNG:

samsungPlayer = new comsamsungutilAudioClip(

comsamsungutilAudioClipTYPE_MMF url);

break;

}

} catch (Exception e) {

eprintStackTrace();

}

}

}

public void play(int loop) {

if (isSupportSound && isSoundOn) {

try {

switch (playerType) {

case NEC:

if (necPlayer == null)

break;

necPlayersetLoopCount(loop);

necPlayerplay();

break;

case STANDARD: //must be dimissed for NECN

if (player == null)

break;

playersetLoopCount(loop);

playerstart();

break;

case NOKIA:

if (nokiaPlayer == null)

break;

nokiaPlayerplay(loop);

break;

case SAMSUNG:

if (samsungPlayer == null)

break;

samsungPlayerplay(loop ); // is volume

break;

}

} catch (Exception e) {

eprintStackTrace();

}

}

}

public void stopSound() {

if (isSupportSound && isSoundOn) {

try {

switch (playerType) {

case STANDARD: //must be dimissed for NECN

if (player == null)

break;

playerdeallocate();

playerstop();

playerclose();

player = null;

break;

case NOKIA:

if (nokiaPlayer == null)

break;

nokiaPlayerstop();

nokiaPlayer = null;

break;

case SAMSUNG:

if (samsungPlayer == null)

break;

samsungPlayerstop();

samsungPlayer = null;

break;

case NEC:

if (necPlayer == null)

break;

necPlayerstop();

necPlayer = null;

break;

}

Systemgc();

} catch (Exception e) {

eprintStackTrace();

}

}

}

}

               

上一篇:如何使用Java模拟.NET的连接池

下一篇:Java反射机制深入研究