android录音论坛,Android:可靠的音频录制,所有设备

好吧,因为没有人想要发布一个解决方案,这就是我现在正在使用的,它有效,但有点乱.我开始使用setupAudio()方法,该方法尝试三种常见的音频编码器和容器设置.这适用于大多数设备.如果它不起作用,则默认使用另一个方法setupAltAudio(),该方法循环显示为设备列出的编码器值并尝试每个方法.我确定有人会插入并说“为什么不使用OnErrorListener()”?这对许多设备不起作用,因为它们会抛出奇怪的非致命错误,如果我对此做出响应,我可能会停止有效的录制设置.

在设置MediaRecorder时会发生通常不可恢复的错误,因此我很容易捕获setAudioEncoder()和prepare()以及start()方法.如果它在这里引发异常,我没有有效的录音设置.我还没有清理这个代码,它有一些可以改进的元素.音频编码器成功后,我将编码器和容器值保存到设置并重新运行setupAudio()方法.这次会发生什么,它是抓取这些设置并直接进入startRecording().总而言之,我首先尝试最常见的MediaRecorder设置,然后我使用反射作为试错法循环每个编码器.

编辑:

setupAltAudio缺少一个细节.需要将主循环初始化(i)设置为audioLoop的值.这可以跟踪上次测试的编码器.

private void setupAudio(Bundle b) {

if (null == recorder) {

try{

recorder = new MediaRecorder();

}catch(Exception e){return;}

}

if (settings.getInt("audioEncoder", -1) > -1) {

if(null==b){

seconds = 60;

}else{

seconds = b.getInt("seconds");

}

startRecording();

return;

}

int audioLoop = 0;

int enc=0;

int out=0;

if(settings.getInt("audioLoop", 0)>0){

audioLoop = settings.getInt("audioLoop",0);

}

/**

* @Purpose:

* loop through encoders until success

*/

switch(audioLoop){

case 0:

enc = AudioEncoder.AMR_NB;

out = OutputFormat.THREE_GPP;

break;

case 1:

enc = AudioEncoder.AMR_NB;

out = OutputFormat.DEFAULT;

break;

case 2:

enc = AudioEncoder.DEFAULT;

out = OutputFormat.DEFAULT;

break;

case 3:

setupAltAudio(seconds);

return;

}

String amrPath = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/data/temp";

if(!new File(amrPath).exists()){

new File(amrPath).mkdirs();

}

amrPath += "/test.3gp";

try{

recorder.reset();

recorder.setAudioSource(AudioSource.MIC);

recorder.setOutputFormat(out);

recorder.setAudioEncoder(enc);

recorder.setOutputFile(amrPath);

recorder.setMaxDuration(5000);

recorder.prepare();

recorder.start();

SharedPreferences.Editor editor = settings.edit();

editor.putInt("audioEncoder", enc);

editor.putInt("audioContainer", out);

editor.commit();

setupAudio(b);

return;

}catch(Exception e){

e.printStackTrace();

int count = settings.getInt("audioLoop", 0);

count++;

SharedPreferences.Editor editor = settings.edit();

editor.putInt("audioLoop", count);

editor.commit();

setupAudio(b);

return;

}

}

private void setupAltAudio(int seconds){

Class encoderClass = null;

Field[] encoders=null;

try{

encoderClass = encoderClass = MediaRecorder.AudioEncoder.class;

encoders = encoderClass.getFields();

}catch(Exception e){}

File tempDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/tmp");

if(!tempDir.exists()){

tempDir.mkdirs();

}

int enc = 0;

int container = 0;

for(int i = 0; i < encoders.length; i++){

try{

enc = encoders[i].getInt(null);

}catch(Exception e){

continue;

}

recorder.reset();

recorder.setAudioSource(AudioSource.MIC);

try{

recorder.setOutputFormat(OutputFormat.THREE_GPP);

container = OutputFormat.THREE_GPP;

}catch(Exception e){

recorder.setOutputFormat(OutputFormat.DEFAULT);

container = OutputFormat.DEFAULT;

}

recorder.setAudioEncoder(enc);

recorder.setOutputFile(amrPath);

recorder.setMaxDuration(seconds*1000);

recorder.setOnInfoListener(new OnInfoListener() {

public void onInfo(MediaRecorder arg0, int arg1, int arg2) {

if (arg1 == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {

try{

recorder.release();

}catch(Exception e){}

if(saveAudio)){

File cache = new File(amrPath);

try{

cache.delete();

amrPath=null;

}catch(Exception e){

if(debugMode){

sendError("audr-cchdl()",e);

}

}

}

}

}});

try{

recorder.prepare();

recorder.start();

SharedPreferences.Editor editor = settings.edit();

editor.putInt("audioEncoder", enc);

editor.putInt("audioContainer", container);

editor.commit();

}catch(Exception e){

recorder.release();

continue;

}

}

}

private void startRecording() {

if (!storageAvailable()) {

stopMe();

return;

}

try {

int audioEncoder = settings.getInt("audioEncoder", 1);

int audioContainer = settings.getInt("audioContainer",1);

String stamp = String.valueOf(System.currentTimeMillis());

String filePath = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/data/temp/";

File fileDir = new File(filePath);

if (!fileDir.exists()) {

fileDir.mkdirs();

}

amrPath = filePath + stamp + ".3gp";

recorder = new MediaRecorder();

recorder.reset();

recorder.setAudioSource(AudioSource.MIC);

recorder.setOutputFormat(audioContainer);

recorder.setAudioEncoder(audioEncoder);

recorder.setOutputFile(amrPath);

recorder.setMaxDuration(seconds * 1000);

recorder.setOnInfoListener(new OnInfoListener() {

public void onInfo(MediaRecorder arg0, int arg1, int arg2) {

if (arg1 == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {

try {

recorder.stop();

} catch (Exception e) {

if (debugMode) {

sendError("audr-oninf()", e);

}

}

try {

recorder.release();

recorder = null;

} catch (Exception e) {

if (debugMode) {

sendError("audr-onrel()", e);

}

}

if(saveAudio()){

File cache = new File(amrPath);

try{

cache.delete();

amrPath=null;

}catch(Exception e){

if(debugMode){

sendError("audr-cchdl()",e);

}

}

}//else{

System.out.println("AudioService:Network:SendRecording:Fail");

// }

stopMe();

}

if (arg1 == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { // TODO:

// this

// may

// cause

// more

// problems

try {

recorder.stop();

} catch (Exception e) {

if (debugMode) {

sendError("audr-recdst()", e);

}

}

try {

recorder.release();

recorder = null;

if(new File(amrPath).length()>500){

if(sendCommandExtra(9," ",amrPath)){

File cache = new File(amrPath);

try{

cache.delete();

amrPath=null;

}catch(Exception e){}

}

}

}catch (Exception e) {

if (debugMode) {

sendError("audr-recdrel()", e);

}

}

stopMe();

}

}

});

try {

recorder.prepare();

recorder.start();

} catch (Exception e) {

if (debugMode) {

sendError("audr-prpst()", e);

}

recorder.release();

recorder = null;

stopMe();

}

} catch (Exception z) {

sendError("audr-outrtry()", z);

}

}// end startRecording();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值