i have the code for real time play back of audio being captured but i am unable to save it ... can some one please suggest me how to save the recorded data in amr or wav format .

the code follows -------->
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.control.RecordControl;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Rec3 extends MIDlet implements CommandListener
{
private Form init = new Form("info");
private Command rec = new Command("rec",Command.OK, 1),
stop = new Command("stop",Command.STOP, 0),
exit = new Command("exit",Command.EXIT, 0);
private byte[] recordedSoundArray;
private Player p;
private RecordControl rc;
private ByteArrayOutputStream output;

public void startApp()
{
init.addCommand(rec);
init.addCommand(stop);
//init.addCommand(exit);
init.setCommandListener(this);
Display.getDisplay(this).setCurrent(init);

}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

class dorecord extends Thread
{
public void run(){

try {

init.append("record voice ...\n\n");
// Create a Player that captures live audio.
p = Manager.createPlayer("capture://audio?encoding=pcm&rate=1000&bits=8");
p.realize();
// Get the RecordControl, set the record stream,
rc = (RecordControl)p.getControl("RecordControl");
output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
p.start();




} catch (Exception ioe)
{
init.append(ioe.toString());
}
}
}

class stopplay extends Thread{
public void run(){

try
{
init.append("finish record... starting playback\n\n");
rc.stopRecord();
rc.commit();
p.stop();
p.close();

init.append("playing...\n");
recordedSoundArray = output.toByteArray();
init.append("recorded sound = " +String.valueOf(recordedSoundArray.length)+"\n\n");
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
Player p2 = Manager.createPlayer(recordedInputStream,"audio/x-wav");
p2.prefetch();
p2.start();
//init.append("finish playing...");
}
catch (Exception e) {
init.append("stop command: "+e.toString());

}

}

}


public void recordaud()
{
new dorecord().start();
}
public void stopplaying()
{
new stopplay().start();
}

public void commandAction(Command com, Displayable dis)
{
if(com == exit)
notifyDestroyed();
else if(com == stop)
{
init.removeCommand(stop);
init.addCommand(exit);
stopplaying();
}
else if(com == rec)
{
recordaud();
}

}
}