hi all
i m getting bad quality of recording for following code of audio recorder, need some help.
Code:/* * RecordVideoCanvas.java * Created on Aug 20, 2008, 10:00:20 AM / package audio; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.file.FileConnection; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.media.Manager; import javax.microedition.media.MediaException; import javax.microedition.media.Player; import javax.microedition.media.control.RateControl; import javax.microedition.media.control.RecordControl; import javax.microedition.media.control.VideoControl; import javax.microedition.media.control.VolumeControl; public class RecordAudioCanvas extends Canvas implements CommandListener{ FileConnection fc; RecordMidlet rmidlet; Player player,player3; RecordControl reorder; VideoControl video; CameraThread cthread ; RecordThread rthread; ViewAudio vaudio; ByteArrayOutputStream bout; byte[] recordArray; private int videoHeight ; private int videoWidth ; private int SPACE = 5; private Command cmdRecord; private Command cmdStop; private Command cmdExit; private Command cmdPlay; public RecordAudioCanvas(RecordMidlet midlet) { rthread = new RecordThread(); cthread = new CameraThread(); vaudio = new ViewAudio(); videoHeight = getHeight(); videoWidth = getWidth(); rmidlet = midlet; cmdRecord = new Command("Record", Command.SCREEN, 0); cmdStop = new Command("Stop", Command.STOP, 0); cmdExit = new Command("Exit", Command.EXIT, 0); cmdPlay = new Command("Play", Command.SCREEN, 1); addCommand(cmdPlay); addCommand(cmdStop); addCommand(cmdRecord); addCommand(cmdExit); setCommandListener(this); cthread.start(); } protected void paint(Graphics g) { g.setColor(0x005945); g.fillRect(0, 0, videoWidth, videoHeight); g.setColor(0xffffff); g.drawLine(SPACE, SPACE, SPACE, videoHeight - SPACE ); g.drawLine(SPACE, SPACE, videoWidth - SPACE, SPACE ); } public void commandAction(Command cmd, Displayable arg1) { if(cmd == cmdRecord){ if(player == null){ cthread.start(); rthread.start(); }else rthread.start(); } if(cmd == cmdExit){ rmidlet.destroyApp(true); } if(cmd == cmdPlay){ vaudio.start(); } if(cmd == cmdStop){ if(reorder != null) stopRecord(); } } private void startAudio(){ try { String message = System.getProperty("supports.recording"); System.out.println(""+message); InputStream is = getClass().getResourceAsStream("/test-wav.wav"); player = Manager.createPlayer(is, "audio/x-wav"); player.setLoopCount(1); player.realize(); VolumeControl volume = (VolumeControl) player.getControl("VolumeControl"); volume.setLevel(20); player.start(); } catch (IOException ex) { ex.printStackTrace(); } catch (MediaException ex) { ex.printStackTrace(); } } class CameraThread extends Thread{ public void run(){ startAudio(); } } class RecordThread extends Thread{ public void run(){ recordAudio(); } } private void recordAudio(){ reorder = (RecordControl) player.getControl("RecordControl"); if(reorder == null){ Alert alert = new Alert("recording","recording not supported" , null, null); alert.setString("not supported"); alert.setTimeout(3000); Display.getDisplay(rmidlet).setCurrent(alert); } System.out.println("record state"+reorder); bout = new ByteArrayOutputStream(); reorder.setRecordStream(bout); reorder.startRecord(); try { Thread.currentThread().sleep(20000); } catch (InterruptedException ex) { ex.printStackTrace(); } try { reorder.stopRecord(); reorder.commit(); } catch (IOException ex) { ex.printStackTrace(); } } private void stopRecord(){ try { reorder.stopRecord(); reorder.commit(); } catch (IOException ex) { ex.printStackTrace(); } } class ViewAudio extends Thread{ public void run(){ viewAudio(); } private void viewAudio(){ if(player!= null) try { player.stop(); player.deallocate(); } catch (MediaException ex) { ex.printStackTrace(); } Alert alert = new Alert("message", bout.toString(), null, null); alert.setTimeout(Alert.FOREVER); Display.getDisplay(rmidlet).setCurrent(alert); ByteArrayInputStream bais = new ByteArrayInputStream(bout.toByteArray()); try { try { System.out.println("supported type is" + System.getProperty("audio.encodings")); System.out.println("video type is"+player.getContentType()); player3 = Manager.createPlayer(bais,"audio/x-wav"); } catch (IOException ex) { ex.printStackTrace(); } catch (MediaException ex) { ex.printStackTrace(); } player3.setLoopCount(1); player3.realize(); player3.prefetch(); RateControl rate = (RateControl)player3.getControl("RateControl"); rate.setRate(200000); } catch (MediaException ex) { ex.printStackTrace(); } try { player3.start(); } catch (MediaException ex) { ex.printStackTrace(); } } } }

Reply With Quote

