JAva j 2 me再生オーディオをキャプチャ

18910 ワード

importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;

importjavax.microedition.lcdui.Alert;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.media.Manager;
importjavax.microedition.media.Player;
importjavax.microedition.media.control.RecordControl;
importjavax.microedition.midlet.MIDlet;

publicclassCapturePlaybackAudioMIDletextendsMIDletimplementsCommandListener{

privateDisplaydisplay=null;

privateAlertalert=newAlert("Message");

privateCommandexitCommand=newCommand("Exit",Command.EXIT,1);

privatePlayercapturePlayer=null;

privatePlayerplaybackPlayer=null;

privateRecordControlrecordControl=null;

privateByteArrayOutputStreambos=newByteArrayOutputStream();

privateByteArrayInputStreambis=null;

publicCapturePlaybackAudioMIDlet(){
display=Display.getDisplay(this);
alert.setTimeout(Alert.FOREVER);
alert.setString("Capturingfor10seconds.");
alert.addCommand(exitCommand);
alert.setCommandListener(this);
try{
capturePlayer=Manager.createPlayer("capture://audio");
if(capturePlayer!=null){
capturePlayer.realize();
recordControl=(RecordControl)capturePlayer
.getControl("javax.microedition.media.control.RecordControl");
display.setCurrent(alert);
if(recordControl==null)
thrownewException("NoRecordControlavailable");
bos=newByteArrayOutputStream(1024);
recordControl.setRecordStream(bos);
}else{
thrownewException("CaptureAudioPlayerisnotavailable");
}
}catch(Exceptione){
error(e);
}
}

publicvoidstartApp(){
try{
capturePlayer.start();
recordControl.startRecord();
Thread.sleep(10000);
recordControl.stopRecord();
recordControl.commit();
capturePlayer.stop();
alert.setString("Welldone!");
bos.flush();
bis=newByteArrayInputStream(bos.toByteArray());
playbackPlayer=Manager.createPlayer(bis,recordControl.getContentType());
playbackPlayer.start();
alert.setString("Playingback...");

}catch(Exceptione){
error(e);
}finally{
try{
if(bos!=null)
bos.close();
if(bis!=null)
bis.close();
}catch(Exceptionex){
error(ex);
}
}

}

publicvoidpauseApp(){
}

publicvoiddestroyApp(booleanunconditional){
}

publicvoidcommandAction(Commandcmd,Displayabledisp){
if(cmd==exitCommand){
notifyDestroyed();
}
}

privatevoiderror(Exceptione){
alert.setString(e.getMessage());
alert.setTitle("Error");
display.setCurrent(alert);
e.printStackTrace();
}

}