elvega
elvega

Reputation: 73

MediaRecorder method start for recorder several audios in Android

I need to record in the same activity several audios, everything works fine, but when I go to record an audio for the second or third time, the app stops and stops working. I don't find fault, testing have seen the error is in the recorder.start line () when I have recorded several audio fails in this function. This is my code: What I can do to fix it?

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();
}

Upvotes: 1

Views: 223

Answers (1)

klijakub
klijakub

Reputation: 845

Try this:

start recording

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();

}

stop recording

 private void stopRecord() {
    try{
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
    }
    catch (RuntimeException stopException){
       ...
    }

}

Upvotes: 1

Related Questions