wordpress适合下载收费的主题,百度关键词排名优化,莆田网站开发公司,科技创新小发明让JavaME程序 Run Anywhere-- 利用反射机制来动态加载声时间:2008-02-23 09:26来源:互联网让JavaME程序 Run Anywhere -- 利用反射机制来动态加载声音API 欢迎指教#xff0c; tengshiquanyahoo.com.cn Write once,run anywhere 是JAVA的口号#xff0c;但在J2ME平台上做的应… 让JavaME程序 Run Anywhere-- 利用反射机制来动态加载声时间:2008-02-23 09:26来源:互联网让JavaME程序 Run Anywhere -- 利用反射机制来动态加载声音API 欢迎指教 tengshiquanyahoo.com.cn Write once,run anywhere 是JAVA的口号但在J2ME平台上做的应用要想不改动代码就run anywhere难度是很大的。如果要把一个应用程序做到让大多数的机型都适用就 让JavaME程序 Run Anywhere-- 利用反射机制来动态加载声音API欢迎指教 tengshiquanyahoo.com.cn 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 MIDIstatic final int NOKIA 1; //For ottstatic final int SAMSUNG 2; //For mmfstatic final int NEC 3; //For MIDI static final String[] supportedPlayerTypes { javax.microedition.media.Player, //STANDARD APIcom.nokia.mid.sound.Sound, // Nokiacom.samsung.util.AudioClip, //samsungcom.nec.media.AudioClip, //nec};下面利用反射机制来动态加载public void determinePlayerType() {// use most - less useisSupportSound true;for (int i 0; i supportedPlayerTypes.length; i ) {// try to load a proper sound Playertry { 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 MIDIcase NEC:createPlayerFactory(/ name .mid);break;case NOKIA: //for ottcreatePlayerFactory(/ name .ott);break;case SAMSUNG: // for mmfcreatePlayerFactory(/ name .mmf);break;}}3。对各个机型特有的BUG是没有什么特别好的办法的只能各个机型调试。这只能怪厂商了。。。。。该方案优点在移植的时候就不用改动代码。只要在相应的机型JAR包中保留相关的资源就可以了。这样就不用为了各个机型都折腾一遍了。注意 用 System.getProperty(microedition.platform)来确定机型是不保险的因为有的机型只是简单地返回J2ME platform。遗留问题1 NecN820 在运行 Class.forName(javax.microedition.media.Player);时候会立刻报“应用程序出错”而不是抛出“ClassNotFoundException”异常。这是该机型JVM的特性BUG所以给NecN820的代码中必须注释掉javax.microedition.media.Player的一切信息。这就得改动代码有违我们的初衷的确是个遗憾。估计NEC的机型都素这样的2 这个类还有待扩展以支持更多机型。并加入震动部分的API。理论上可以包含所有的机型。但实际应用中只要包含需要用到的机型相关API就可以了。测试机型 在 三星E708MOTOV600NOKIA 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 tengshiquanyahoo.com.cn* * 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 MIDIstatic final int NOKIA 1; //For ottstatic final int SAMSUNG 2; //For mmfstatic final int NEC 3; //For MIDIstatic final String[] supportedSoundTypes { mid, mmf, ott, };private static int soundType;static final String[] supportedPlayerTypes {javax.microedition.media.Player, //must be dimissed for NECN820com.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 gamestatic boolean isSupportSound;static boolean isSoundOn true;// ********************* STANDARD ***********************Player player;// ********************* NOKIA ***********************Sound nokiaPlayer;// ********************* SAMSUNG ***********************com.samsung.util.AudioClip samsungPlayer;// ********************* NEC ***********************com.nec.media.AudioClip necPlayer;// singletonprivate 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 useisSupportSound true;for (int i 0; i supportedPlayerTypes.length; i ) {// try to load a proper sound Playertry {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 MIDIcase NEC:createPlayerFactory(/ name .mid);break;case NOKIA: //for ottcreatePlayerFactory(/ name .ott);break;case SAMSUNG: // for mmfcreatePlayerFactory(/ 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 NECN820is 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 NECN820if (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 volumebreak;}} catch (Exception e) {e.printStackTrace();}}}public void stopSound() {if (isSupportSound isSoundOn) {try {switch (playerType) {case STANDARD: //must be dimissed for NECN820if (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();}}}} 转载于:https://blog.51cto.com/zhaohaiyang/454916