MMAPI播放声音简例

来源:互联网 发布:淘宝做大码女装退货率 编辑:程序博客网 时间:2024/06/14 13:31

用手机播放声音。进行多媒体操作。现在用倒推的形式来分析。步骤如下:

 

 

首先,播放声音要用到一个播放声音的类,用这个的功能是完成播放、暂停、停止等功能的。在MMAPI中此类为Player。有了这个类的对象,很容易播放声音。代码如下:

(Player player;)


player.realize();

player.start();

player.stop();

 

 

我们现在知道了播放声音的方法。现在有了一个问题,player是从哪里得到呢?这个player不是new出来的,而是用Manager创建的。

 

Manager有下面这样一个函数,public static Player createPlayer(java.io.InputStream stream, java.lang.String type)。它可以用来创建一个Player的实例。现在来看它的两个参数,一个类型是InputStream,一个类型是String

 

根据现在的理解,可以知道,前一个参数应该是一个声音的输入流,后面的参数则指定了这个声音的类型。

 

根据MMAPI所提供的参考来看,声音类型共有下面几种:

1. Wave audio files: audio/x-wav

2. AU audio files: audio/basic

3. MP3 audio files: audio/mpeg

4. MIDI files: audio/midi

5. Tone sequences: audio/x-tone-seq

6. MPEG video files: video/mpeg

 

 

声音输入流的取得也非常的容易。只需要知道声音文件的位置,用InputStream is = getClass().getResourceAsStream(src);取得即可。

 

 

这样,我们就播放了一个声音文件,其余的工作就是控制播放了。

 

 

最后,我们要保证程序能够找到指定的声音文件就可以了。在eclipse3.0+eclipseme中,载入资源文件的默认路径和.class文件同目录。

/*
 * Created on 2005-3-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package WaveSound;

import java.io.InputStream;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PlayerWave extends MIDlet implements CommandListener{

 private Command  playCommand = new Command("Play", Command.ITEM, 1);
 private Command  exitCommand = new Command("Exit", Command.EXIT, 1);
 
 private List theList = new List("MIDP Audio Player", Choice.IMPLICIT);
 private Display display;
 
 private String strWave = "bark.wav";
 private String strSoundType = "audio/x-wav";
 
 private Player player = null;
 
 /**
  *
  */
 public PlayerWave() {
  super();
  // TODO Auto-generated constructor stub
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#startApp()
  */
 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  theList.addCommand(playCommand);
  theList.addCommand(exitCommand);
  theList.setCommandListener(this);
  
  display = Display.getDisplay(this);
  display.setCurrent(theList);
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#pauseApp()
  */
 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
  */
 protected void destroyApp(boolean arg0){
  // TODO Auto-generated method stub

 }

 /* (non-Javadoc)
  * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
  */
 public void commandAction(Command c, Displayable d) {
  // TODO Auto-generated method stub
  if(c == exitCommand){
   StopSound();
   destroyApp(true);
   notifyDestroyed();
  } else if(c == playCommand){
   PlayWaveSound();
  }
 }

 /**
  *
  */
 private void StopSound() {
  // TODO Auto-generated method stub
  if(player != null){
   try{
    player.stop();
   }catch(Exception e){
    
   }
   
   player = null;
  }
 }

 /**
  *
  */
 private void PlayWaveSound() {
  // TODO Auto-generated method stub
  InputStream is = getClass().getResourceAsStream(strWave);
  
  if(player != null){
   StopSound();
  }
  
  try{
   player = Manager.createPlayer(is, strSoundType);
  }catch(Exception e){
   ShowInfo("Error", "Play Sound Error1!");
   player = null;
   return;
  }
  
  try{
   player.realize();
  }catch(Exception e){
   ShowInfo("Error", "Play Sound Error2!");
   player = null;
   return;
  }
  
  try{
   player.start();
  }catch(Exception e){
   ShowInfo("Error", "Play Sound Error3!");
   player = null;
   return;
  }
  
  ShowInfo("Sucess", "Play sound sucess!");
 }
 
 private void ShowInfo(String strType, String strInfos){
  if(strType.length() <= 0 ){
   strType = "WaveSound Info";
  }
  
  if(strInfos.length() <= 0){
   return;
  }
  
  Alert at = new Alert(strType, strInfos, null, null);
  at.setTimeout(3000);
  display.setCurrent(at);
 }

}

原创粉丝点击