我需要在同一个 Activity 中录制多个音频,一切正常,但是当我第二次或第三次录制音频时,应用停止并停止工作。我没有发现错误,测试发现错误是在 recorder.start line () 当我在这个函数中录制了几个音频失败时。这是我的代码:我能做些什么来修复它?
public String grabar(View v,String namefile) {
String url;
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//configura el micro del móvil como fuente
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//formato audio salida
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//especifica el codel del audio
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setMaxDuration(20000);//graba máximo 20 segundos
File path = new File(Environment.getExternalStorageDirectory()//obtenemos el path de la sd y creamos un archivo de extensión 3gp
.getPath());
Log.d("uri1",path.toString());
try {
archivo = File.createTempFile(namefile, ".3gp", path);
} catch (IOException e) {
Log.d("uri1x","error creando fichero");
}
Log.d("uri2",namefile);
Log.d("uri3",archivo.toString());
url=archivo.toString();
recorder.setOutputFile(archivo.getAbsolutePath());//donde se almacena
Log.d("uri4", archivo.getAbsolutePath().toString());
try {//prepara la grabación
recorder.prepare();
} catch (IOException e) {
Log.e("uri5", "Fallo en grabación");
}
Log.d("uri6", archivo.getAbsolutePath().toString());
recorder.start();//graba
Log.d("uri7", archivo.getAbsolutePath().toString());
Toast.makeText(this, "Grabando...", Toast.LENGTH_SHORT).show();
b1.setEnabled(false);
b2.setEnabled(true);*/
isRecording = true;
return url;
}
public void detener(View v) {
if(isRecording) {
isRecording=false;
recorder.stop();
recorder.reset();
recorder = null;
Toast.makeText(this, "Grabación terminada", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
recorder.release();
}
最佳答案
试试这个:
开始录音
private void startRecord() throws IOException {
if(mMediaRecorder!=null){
mMediaRecorder.release();
}
FILENAME = Environment.getExternalStorageDirectory() + "/" + timestamp + ".mp4";
File fileOut = new File(FILENAME);
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
mMediaRecorder.setOutputFile(FILENAME);
mMediaRecorder.prepare();
mMediaRecorder.start();
}
停止录音
private void stopRecord() {
try{
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
}
catch (RuntimeException stopException){
...
}
}
关于android - MediaRecorder 方法启动,用于在 Android 中录制多个音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597544/