سلام دوستان. صبح همگی به خیر . نماز و روزه هاتون قبول.
من تو برنامم نیاز دارم که صدایی رو رکورد کنم و سپس پخش کنم و در نهایت ذخیره کنم . کد مربوط به رکورد و پخش رو دارم. که در زیر گذاشتم:
class VoiceRecordForm extends Form implements CommandListener {
private StringItem message;
private StringItem errormessage;
private final Command record, play;
private Player player;
private byte[] recordedAudioArray = null;
public VoiceRecordForm() {
super("Recording Audio");
message = new StringItem("", "Select Record to start recording.");
this.append(message);
errormessage = new StringItem("", "");
this.append(errormessage);
record = new Command("Record", Command.OK, 0);
this.addCommand(record);
play = new Command("Play", Command.BACK, 0);
this.addCommand(play);
this.setCommandListener(this);
}
public void commandAction(Command comm, Displayable disp) {
if (comm == record) {
Thread t = new Thread() {
public void run() {
try {
player = Manager.createPlayer("capture://audio?encoding=pcm");
player.realize();
RecordControl rc = (RecordControl) player.getControl("RecordControl");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
player.start();
message.setText("Recording...");
Thread.sleep(5000);
message.setText("Recording Done!");
rc.commit();
recordedAudioArray = output.toByteArray();
player.close();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
};
t.start();
}
else if (comm == play) {
try {
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedAudioArray);
Player p2 = Manager.createPlayer(recordedInputStream, "audio/basic");
p2.prefetch();
p2.start();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
}
حالا برای ذخیره سازی یه کد دارم ولی کار نمیکنه ولی قرار می دم اگه وقت یه نگاهی بندازید
public boolean writeFile()
{
javax.microedition.io.Connection c = null;
java.io.OutputStream os = null;
try {
String src="file:///E:/YourDIR" ;
c = javax.microedition.io.Connector.open(src, javax.microedition.io.Connector.READ_WRITE);
javax.microedition.io.file.FileConnection fc =
(javax.microedition.io.file.FileConnection) c;
if(!fc.isDirectory())
{
fc.mkdir();
}
fc.close();
c.close();
c = Connector.open(src+"/a1.amr", Connector.READ_WRITE);
fc = (FileConnection) c;
if(!fc.exists())
fc.create();
else
fc.truncate(0);
os = fc.openOutputStream();
OutputStream out = fc.openOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len =recordedInputStream.read(buf)) > 0)
{
recordedInputStream.read(buf);
out.write(buf);
}
out.close();
return true;
} catch (Exception e) {
String st1=e.getMessage();
return false;
} finally {
try {
if (os != null)
{
os.close();
}
if (c != null)
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}