android - MediaRecorder 方法启动,用于在 Android 中录制多个音频

标签 android audio android-mediarecorder

我需要在同一个 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/

相关文章:

android - 让MediaPlayer在启动新的Intent时继续播放

android - 如何使用 Mediarecorder 捕获像 instagram 和 vine 这样的视频广场

java - 嵌套对象的改造解析

c# - 在Windows Phone 8上使用SD卡

java - android studio 改变 API<23 的音频播放速度

php - 有没有一种方法可以计算单击音频按钮的次数,并使用ajax调用将其保存到数据库中?

Android 通话录音 来电未录音

android - 具有多个帐户的应用内购买

Android:Mediarecorder 在 start() 中抛出 IllegalStateException

android - 使用 MediaRecorder 录制 mp4 时获取/修改帧