首页 arrow J2ME开发 arrow 程序设计 arrow J2ME读取WEB上的声音文件
J2ME读取WEB上的声音文件
Author Author: Wupei | Date Date: 2009-10-31 | View Count View: 2268 | Section & Category J2ME开发 - 程序设计 | Digg Digg: 0
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) {
   }
}

更多阅读:

 

尚无评论发表

我要发表评论

登录菜单

最新文章

本月热门

订阅本站

RSS 0.91 RSS 1.0 RSS 2.0 ATOM 0.3 OPML