📄 mmapimidlet.java
字号:
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class MMAPIMIDlet extends MIDlet implements CommandListener,PlayerListener{
private static final Command CMD_EXIT=new Command("退出",Command.EXIT,2);
private static final Command CMD_MONOPLAY=new Command("单音播放",Command.OK,1);
private static final Command CMD_SIMPLEPLAY=new Command("简单重放控制",Command.OK,1);
private static final Command CMD_DETAILPLAY=new Command("详细重放控制",Command.OK,1);
private static final Command CMD_MIDIPLAY=new Command("MIDI重放控制",Command.OK,1);
private static final Command CMD_VIDEOPLAY=new Command("视频播放",Command.OK,1);
private static final Command CMD_MONOSEQUENCE=new Command("单音序列",Command.OK,1);
private Display display;
private Form mainForm;
private static final long SECOND_TO_MICROSECOND=1000000L;
public MMAPIMIDlet() {
// TODO Auto-generated constructor stub
display=Display.getDisplay(this);
mainForm=new Form("多媒体编程");
mainForm.addCommand(CMD_EXIT);
mainForm.addCommand(CMD_MONOPLAY);
mainForm.addCommand(CMD_SIMPLEPLAY);
mainForm.addCommand(CMD_DETAILPLAY);
mainForm.addCommand(CMD_MIDIPLAY);
mainForm.addCommand(CMD_VIDEOPLAY);
mainForm.addCommand(CMD_MONOSEQUENCE);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
/*String[] strFormat=Manager.getSupportedContentTypes(null);
for(int index=0;index<strFormat.length;index++)
{
System.out.println(strFormat[index]);
}*/
String[] strProtocol=Manager.getSupportedProtocols(null);
for(int index=0;index<strProtocol.length;index++)
{
System.out.println(strProtocol[index]);
String[]strFormat=Manager.getSupportedContentTypes(strProtocol[index]);
for(int script=0;script<strFormat.length;script++)
{
System.out.println(strFormat[script]);
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
public void commandAction(Command c,Displayable d)
{
if(c==CMD_EXIT)
{
notifyDestroyed();
}
else if(c==CMD_MONOPLAY)
{
try
{
Manager.playTone(ToneControl.C4,5000,100);
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
else if(c==CMD_SIMPLEPLAY)
{
try
{
InputStream is=this.getClass().getResourceAsStream("/pattern.mid");
Player player=Manager.createPlayer(is,"audio/midi");
System.out.println("简单重放控制播放器创建成功");
player.realize();
player.prefetch();
player.setLoopCount(5);
player.start();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
ioe.printStackTrace();
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
else if(c==CMD_DETAILPLAY)
{
Player player;
VolumeControl volumeControl;
try
{
InputStream is=this.getClass().getResourceAsStream("/ALLMYLOV.MID");
player=Manager.createPlayer(is,"audio/midi");
System.out.println("详细重放控制播放器创建成功");
player.realize();
player.addPlayerListener(this);
volumeControl=(VolumeControl)player.getControl("VolumeControl");
if(volumeControl!=null)
{
volumeControl.setLevel(100);
}
player.setMediaTime(5*SECOND_TO_MICROSECOND);
player.prefetch();
player.start();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
ioe.printStackTrace();
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
else if(c==CMD_MIDIPLAY)
{
Player player;
TempoControl tempoControl;
try
{
InputStream is=this.getClass().getResourceAsStream("/pattern.mid");
//player=Manager.createPlayer("/pattern.mid");
player=Manager.createPlayer(is,"audio/midi");
System.out.println("MIDI重放控制播放器成功创建播放器");
player.realize();
tempoControl=(TempoControl)player.getControl("TempoControl");
tempoControl.setTempo(120000);
player.start();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
ioe.printStackTrace();
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
else if(c==CMD_VIDEOPLAY)
{
Player player;
VideoControl videoControl;
try
{
InputStream is=this.getClass().getResourceAsStream("/movie.mpg");
//player=Manager.createPlayer("/Firebathero.3gp");
player=Manager.createPlayer(is,"video/mpeg");
System.out.println("视频播放器创建成功");
player.realize();
videoControl=(VideoControl)player.getControl("VideoControl");
if(videoControl!=null)
{
//Form form=new Form("视频播放器");
//form.append((Item)videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null));
mainForm.append((Item)videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null));
display.setCurrent(mainForm);
}
player.start();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
ioe.printStackTrace();
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
else if(c==CMD_MONOSEQUENCE)
{
byte tempo = 30; // set tempo to 120 bpm
byte depth = 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,depth, D4,depth, C4,depth, E4,depth, // content of "A" section
E4,depth, E4,depth, E4,depth, rest,depth,
ToneControl.BLOCK_END, 0, // end define "A" section
ToneControl.PLAY_BLOCK, 0, // play "A" section
D4,depth, D4,depth, D4,depth, rest,depth, // play "B" section
E4,depth, G4,depth, G4,depth, rest,depth,
ToneControl.PLAY_BLOCK, 0, // repeat "A" section
D4,depth, D4,depth, E4,depth, D4,depth, C4,depth // play "C" section
};
try{
Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
p.realize();
ToneControl control = (ToneControl)p.getControl("ToneControl");
control.setSequence(mySequence);
p.start();
} catch (IOException ioe) {
} catch (MediaException me) { }
}
}
public void playerUpdate(Player player,String event,Object eventData)
{
if(event=="EVENT_OF_MEDIA" || event=="STOP_AT_TIME")
{
System.out.println("处理完毕");
try
{
player.setMediaTime(5*SECOND_TO_MICROSECOND);
player.start();
}
catch(MediaException me)
{
System.out.println(me.toString());
me.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -