使用MIDP发出一个简单的音调非常简单,只需使用 Manager 类的静态方法 playTone 就可以完成。方法的定义如下:
public static void playTone(int note, int duration, int volume)
note表示单音符,取值范围0~127
duration表示持续播放此音符的时间
volume表示音量,范围是0~100
在本示例中,给出一个简单的实例,说明简单音调的播放方法。下面是程序源码:
package zieckey.j2me.study.mmapi;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.media.Manager;
/**
*
* 本程序给出播放简单音调的示例。
*
* 在程序界面上放置了音调和音量的控制,这是通过Gauge控件完成的。
* 另外,还可以通过一个TextField控件实现播放时间的控制。
*
* @author zieckey
* @version 2008.4.26
*
*/
public class PlayToneMIDlet extends MIDlet implements CommandListener
{
private Display display;
private Command playCommand;
private Command exitCommand;
private Form form;
private Gauge noteGauge;
private Gauge volumeGauge;
private TextField durationTextField;
public PlayToneMIDlet( )
{
display = Display.getDisplay( this );
playCommand = new Command( "播放", Command.SCREEN, 1 );
exitCommand = new Command( "退出", Command.SCREEN, 1 );
form = new Form( "播放音调示例" );
//Gauge(String label, boolean interactive, int maxValue, int initialValue)
noteGauge = new Gauge( "音调", true, 127, 60 );
volumeGauge = new Gauge( "音量", true, 100, 63 );
durationTextField = new TextField( "持续时间", "1000", 10, TextField.NUMERIC );
form.append( noteGauge );
form.append( volumeGauge );
form.append( durationTextField );
form.addCommand( playCommand );
form.addCommand( exitCommand );
form.setCommandListener( this );
}
protected void startApp() throws MIDletStateChangeException
{
display.setCurrent( form );
}
protected void destroyApp( boolean arg0 ) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
public void commandAction( Command cmd, Displayable d )
{
// 对该命令的响应
if ( cmd == exitCommand )
{
try
{
destroyApp( true );
} catch ( MIDletStateChangeException e )
{
e.printStackTrace();
}
notifyDestroyed();
} else if ( cmd == playCommand )
{
try
{
/**
* Player的VolumeControl数值为0~100
*
* note表示单音符,取值范围0~127
* duration表示持续播放此音符的时间
* volume表示音量,范围是0~100
* Manager.playTone(int note, int duration, int volume)
*/
Manager.playTone( noteGauge.getValue(),
Integer.parseInt( durationTextField.getString() ),
volumeGauge.getValue() );
} catch ( Exception e )
{
e.printStackTrace();
}
}
}
}
|
阅读(2706) | 评论(0) | 转发(0) |