读书人

一个J2ME在nokia手机上录音的有关问题

发布时间: 2012-01-07 21:41:55 作者: rapoo

请教各位一个J2ME在nokia手机上录音的问题。
请教各位一个J2ME在nokia手机上录音的问题。
代码片断如下

try {
ByteArrayOutputStream o1;
int x=0,size=0;
byte[] buf1 = new byte[32000];
o1= new ByteArrayOutputStream(32000 );

Player p = Manager.createPlayer( "capture://audio?encoding=pcm&rate=11025&bits=16&channels=1 ");

p.realize();
RecordControl rc = (RecordControl)p.getControl( "RecordControl ");
while(1==1)
{




o1.reset();
rc.setRecordStream(o1);
rc.setRecordSizeLimit(24000);

rc.startRecord();

p.start();

Thread.currentThread().sleep(1000);

p.stop();

rc.stopRecord();

rc.commit();

{ x= o1.size();
size=x;
buf1= o1.toByteArray();
//.... 其他代码
}

}
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException e) { }

该程序在模拟器上执行是OK 的可以连续运行很久都没有问题
但load 到手机上之后 while 循环600多次 (7610上,N73上是459次)之后,
程序就不能再从mic获得音频数据了。o1.size 总是为0,
这个时候就算退出 while循环,重新初始化 Player 也不能成功。请问各位是否有
遇到过这种情况呢? 是 程序 在内存管理上有问题?还是 player 不能这样用法?


[解决办法]
这里不要循环处理的,只要把RecordControl.startRecord()和Player.start()启动一次就可以了。我的录音处理就很简单,当点击录音按钮时,则启动一次如下方法:
public void startRecord(){
try {
this.close();//首先关闭player
this.soundPlayer = Manager.createPlayer( "capture://audio?encoding=audio/amr ");
output = new ByteArrayOutputStream();//用于保存录下的数据
this.soundPlayer.realize();
this.rc = (RecordControl) this.soundPlayer.getControl( "RecordControl ");
this.rc.setRecordStream(output);
this.rc.startRecord();
this.soundPlayer.start();

} catch (Exception ex) {
ex.printStackTrace();
}
}
在停止录音的时候,调用如下方法:
public void stopRecord(){
try {
if (this.soundPlayer != null && this.soundPlayer.getState() != Player.CLOSED) {
this.soundPlayer.stop();


this.rc.stopRecord();
this.rc.commit();
this.rc = null;
ByteArrayInputStream bis=new ByteArrayInputStream(output.toByteArray());
output.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

读书人网 >J2ME开发

热点推荐