Today, I and Marlon started our thesis programming together. We were able to now record and play audio. Thanks to our joined power! hehe:) We were able to fix the code by creating a separate thread for recording…Today, we also learned about the different audio formats…
package record;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.control.*;
import javax.microedition.media.*;
import java.io.*;
/**
* This form shows how the cellphone’s microphone can be used to record audio and how the recorded audio can be played on the cellphone. The original author of this code is Alexander De Luca. There are some problems regarding possible deadlock when recording audio. As a solution, we opted to create another thread just handling the recording of audio.
* modified on November 18, 2006
*/
public class RecordForm extends Form implements CommandListener{
private StringItem messageItem;
private StringItem errorItem;
private final Command recordCommand, playCommand;
private Player p;
private byte[] recordedSoundArray = null;
public RecordForm() {
super(”Record Audio”);
messageItem = new StringItem(”Record”, “Click record to start recording.”);
this.append(messageItem);
errorItem = new StringItem(”", “”);
this.append(errorItem);
recordCommand = new Command(”Record”, Command.SCREEN, 1);
this.addCommand(recordCommand);
playCommand = new Command(”Play”, Command.SCREEN, 2);
this.addCommand(playCommand);
StringBuffer inhalt = new StringBuffer();
this.setCommandListener(this);
}
public void commandAction(Command comm, Displayable disp) {
if(comm==recordCommand) {
try {
Record record1= new Record(); //thread
record1.start();
} catch(Exception e){
System.err.println(e);
}
} else if(comm == playCommand) {
try {
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
Player p2 = Manager.createPlayer(recordedInputStream,”audio/basic”);
p2.prefetch();
p2.start();
} catch (IOException ioe) {
errorItem.setLabel(”Error”);
errorItem.setText(ioe.toString());
} catch (MediaException me) {
errorItem.setLabel(”Error”);
errorItem.setText(me.toString());
}
}
}
public class Record extends Thread
{
private boolean quit=false;
public void run ()
{
while (!quit)
{
try
{
// create the Player object using amr encoding
p=Manager.createPlayer(”capture://audio?encoding=pcm”);
p.realize();
// get the RecordControl over this Player
RecordControl rc = (RecordControl)p.getControl(”RecordControl”);
// create an OutputStream which the RecordControl will use
// to write write the recorded data.
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
// start the recording
rc.startRecord();
p.start();
messageItem.setText(”recording…”);
Thread.currentThread().sleep(5000); //record for 5 seconds
messageItem.setText(”done!”);
rc.commit();
// save the recordedData in a byte array
recordedSoundArray = output.toByteArray();
// close the player
p.close();
quit=true;
} catch (IOException ioe) {
errorItem.setLabel(”Error”);
errorItem.setText(ioe.toString());
} catch (MediaException me) {
errorItem.setLabel(”Error”);
errorItem.setText(me.toString());
} catch (InterruptedException ie) {
errorItem.setLabel(”Error”);
errorItem.setText(ie.toString());
}
}
}
}
}
Hai Sir,
I am a B.E student and now i am doing my B.E project on database management of students using visual basic 6.0. as front end and MS Access for storing database.
Now, I want to add VOICE RECORDING(audio profile) of individual members in that project.I don’t know how to do it.Can you please send me the idea and source code so that i can effectively enhance my project.
Thank you,
Posted by Ramachandran.P at August 21, 2008, 7:00 pmhi… i am amey.
i am trying to execute your code on Nokia 6233 mobile but i got error like Javax.microedition.midlet.MediaException:invalid encoding… like this so could you please tell me that whether this code will execute on any mobile which have support for JSR 82 and JSR 135 after generating the jar file. cause i having a problem in yhat…. please tell me
All comments are moderated. Your comments will not appear here unless approved by the blog owner. Thank you.
Hai Sir,
I am ramachandaran,from salem,tamilnadu,INDIA.I am a B.E student and now i am doing my B.E project on database management of students using visual basic 6.0. as front end and MS Access for storing database.
Now, I want to add VOICE RECORDING(audio profile) of individual members in that project.I don’t know how to do it.Can you please send me the idea and source code so that i can effectively enhance my project.
Thank you,
Posted by Ramachandran.P at August 21, 2008, 6:59 pm