import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; import javax.microedition.io.*; import java.io.*; public class WEBSound extends MIDlet implements Runnable, CommandListener { private Player player = null; private Thread soundThread; public void startApp() { this.soundThread = new Thread(this); this.soundThread.start(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { this.notifyDestroyed(); } public void run() { try { String URL = "http://www.yiyim.com/music/my.wav"; InputStream is = loadSound(URL); //获取服务器声音文件 player = Manager.createPlayer(is, "audio/x-wav"); player.start(); } catch (Exception ioe) { System.out.println("出错了:" + ioe.toString()); } } public InputStream loadSound(String url) throws IOException { HttpConnection hpc = null; InputStream is = null; try { hpc = (HttpConnection) Connector.open(url); is = hpc.openInputStream(); return is; } finally { if (hpc != null) { hpc.close(); } } } }
下面这个也行:
=========================
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; import java.io.*; public class SoundMIDlet extends MIDlet { private Player player = null; public SoundMIDlet() { try { player = Manager.createPlayer("http://www.yiyim.com/music/my2.wav"); } catch (IOException e) { System.out.println("1:" + e); } catch (MediaException e) { System.out.println("2:" + e); } catch (Exception e) { System.out.println("3:" + e); } } public void startApp() { if (player != null) { try { player.start(); } catch (MediaException e) { System.out.println("4:" + e); } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }