j2meblog

symbian

j2me播放音频的代码

j2me播放音频的代码import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import java.io.*;

public class PlayWav_2 extends MIDlet implements CommandListener {
 private Display display;

 private Form mainForm; // Main form

 private Command exitCommand; // Command to exit the MIDlet

 private Command playCommand; // Command to play wav file

 public PlayWav_2() {
  display = Display.getDisplay(this);
  
  exitCommand = new Command("Exit", Command.EXIT, 1);
  playCommand = new Command("Play", Command.SCREEN, 2);
  
  mainForm = new Form("Play WAV file");
  mainForm.addCommand(exitCommand);
  mainForm.addCommand(playCommand);
  mainForm.setCommandListener(this);
 }

 public void startApp() {
  display.setCurrent(mainForm);
 }

 public void pauseApp() {
 }

 public void destroyApp(boolean unconditional) {
 }

 
 public void commandAction(Command c, Displayable s) {
  if (c == exitCommand) {
   destroyApp(false);
   notifyDestroyed();
  } else if (c == playCommand)
   playwavfile();
 }

 
 public void playwavfile() {
  try {
   // 从Jar包中读取wav文件
   InputStream in = getClass().getResourceAsStream("/test.wav");
   //指定文件类型
   Player p = Manager.createPlayer(in, "audio/x-wav");
                        p.realize();
                        p.prefetch(); 
   p.start();                       
                        if(p.getState()==300){p.close();}
  } catch (Exception e) {

   Alert alr = new Alert("错误", "不能够播放 WAV 文件!", null,
     AlertType.ERROR);
   alr.setTimeout(Alert.FOREVER);
   display.setCurrent(alr, mainForm);
  }
 }
}

http://www.j2mehome.com/

 

posted on 2009-07-23 21:14 j2meer 阅读(231) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。