1、按照必定的格局讀取聲音文件
2、將讀取到的內容傳遞給播放器
3、播放聲音
應用Player對象的start方法,可以將聲音播放出來,示例代碼:player.start();
在播放聲音時也可以設定聲音播放的次數,可以應用Player類中的setLoopCount方法來實現,具體可查閱API文檔。下面是在NOKIA S60模仿器中測試通過。代碼如下:
package sound;
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
import Javax.microedition.media.*;
import Java.io.*;
public class SoundMIDlet extends MIDlet
{
private Player player = null;
/** Constructor */
public SoundMIDlet()
{
try
{
InputStream is = this.getClass()
.getResourceAsStream("/Autorun.wav");
player = Manager.createPlayer(is, "audio/x-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);
}
}
/** Main method */
public void startApp()
{
if (player != null)
{
try
{
player.start();
}
catch (MediaException e)
{
System.out.println("4:" + e);
}
}
}
/** Handle pausing the MIDlet */
public void pauseApp()
{
}
/** Handle destroying the MIDlet */
public void destroyApp(boolean unconditional)
{
}
}