JAva j 2 meネットワークプレーヤー管理

19367 ワード

importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Form;
importjavax.microedition.lcdui.List;
importjavax.microedition.lcdui.StringItem;
importjavax.microedition.media.Manager;
importjavax.microedition.media.Player;
importjavax.microedition.midlet.MIDlet;

classThreadedMIDletextendsMIDletimplementsCommandListener{

privateListlist=newList("PressPlay",List.IMPLICIT);

privateDisplaydisplay;

privateNetworkPlayerManagermgr;

privateCommandcancel=newCommand("Cancel",Command.CANCEL,1);

publicThreadedMIDlet(){
list.addCommand(newCommand("Exit",Command.EXIT,1));
list.addCommand(newCommand("Play",Command.SCREEN,1));

list.setCommandListener(this);
display=Display.getDisplay(this);
}

publicvoidstartApp(){
display.setCurrent(list);
}

publicvoidpauseApp(){
}

publicvoiddestroyApp(booleanunconditional){
}

publicvoidcommandAction(Commandcmd,Displayabledisp){
if(cmd.getLabel().equals("Exit")){
notifyDestroyed();
}elseif(cmd.getLabel().equals("Cancel")){
mgr.cancel();
display.setCurrent(list);
}else{
try{
mgr=newNetworkPlayerManager(display,cancel,this);
Threadrunner=newThread(mgr);
runner.start();
}catch(Exceptione){
System.err.println(e);
}
}
}
}

publicclassNetworkPlayerManagerimplementsRunnable{

privateDisplaydisplay;

privateFormform=newForm("NetworkPlayerManager");

privateStringItemmsg=newStringItem("PleaseWait...",null);

privatebooleancancel=false;

privatePlayerplayer=null;

publicNetworkPlayerManager(Displaydisplay,CommandcancelCmd,ThreadedMIDletparent){
this.display=display;
form.append(msg);
form.addCommand(cancelCmd);

form.setCommandListener(parent);
}

publicvoidrun(){
display.setCurrent(form);
booleanconnected=false;

try{
player=Manager.createPlayer("http://www.yoursite.com/s.wav");
player.realize();
connected=true;
}catch(Exceptione){
msg.setText("Failed:"+e.getMessage());
System.err.println(e);
return;
}

if(connected&&!cancel)
msg.setText("Connected.Startingplayback...");
else{
msg.setText("Unabletoconnect.");
return;
}
try{
player.start();
}catch(Exceptionpe){
msg.setText(pe.getMessage());
}
}
publicvoidcancel(){
cancel=true;
if(player!=null)
player.deallocate();
}
}