📄 playsimpletone_2.java
字号:
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class PlaySimpleTone_2
extends MIDlet implements CommandListener {
private Player mp;
private Display display;
private List list;
private Command exitCommand = new Command("Exit", Command.EXIT, 2);
private Command playCommand = new Command("Play", Command.ITEM, 1);
private Command stopCommand = new Command("Stop", Command.ITEM, 1);
byte tempo = 30; // set tempo to 120 bpm
byte d = 8; // eighth-note
byte C4 = ToneControl.C4;;
byte D4 = (byte)(C4 + 2); // a whole step
byte E4 = (byte)(C4 + 4); // a major third
byte G4 = (byte)(C4 + 7); // a fifth
byte rest = ToneControl.SILENCE; // rest
byte[] mySequence = {
ToneControl.VERSION, 1, // version 1
ToneControl.TEMPO, tempo, // set tempo
ToneControl.BLOCK_START, 0, // start define "A" section
E4,d, D4,d, C4,d, E4,d, // content of "A" section
E4,d, E4,d, E4,d, rest,d,
ToneControl.BLOCK_END, 0, // end define "A" section
ToneControl.PLAY_BLOCK, 0, // play "A" section
D4,d, D4,d, D4,d, rest,d, // play "B" section
E4,d, G4,d, G4,d, rest,d,
ToneControl.PLAY_BLOCK, 0, // repeat "A" section
D4,d, D4,d, E4,d, D4,d, C4,d // play "C" section
};
private Player p=null;
public PlaySimpleTone_2() {
display = Display.getDisplay(this);
list = new List("播放曲调例子", List.IMPLICIT);
}
public void startApp() {
list.addCommand(exitCommand);
list.addCommand(stopCommand);
list.setCommandListener(this);
display.setCurrent(list);
try{
p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
p.realize();
ToneControl c = (ToneControl)p.getControl("ToneControl");
c.setSequence(mySequence);
p.start();
} catch (IOException ioe) {
} catch (MediaException me) { }
}
public void destroyApp(boolean unconditional) {
}
public void pauseApp() {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
if(p!= null)
p.close();
destroyApp(false);
notifyDestroyed();
}
if (c == playCommand) {
if(p!= null){
try{
p.start();
}catch (MediaException me) { }
}
list.removeCommand(playCommand);
list.addCommand(stopCommand);
display.setCurrent(list);
}
if (c == stopCommand) {
if(p!= null){
try{
p.stop();
}catch (MediaException me) { }
}
list.removeCommand(stopCommand);
list.addCommand(playCommand);
display.setCurrent(list);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -