Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mediarecorder stop failed

I've faced a very strange behavior: sometimes my mediarecorder crashes with an error "Stop failed" and sometimes it works fine. Is there my fault or it is a bug of the system? I cant't get what is wrong.

private void stopRecording(){
        ticker.cancel();
        ticker.purge();

        recorder.stop();

        startBtn.setText("Start");
        recordInProcess = false;

        markList = locWriteTask.getMarkArray();

    mCamera.lock();
        recorder.release();
    }

 private void startRecording(){

         startBtn.setText("Stop");

         recordInProcess = true;

             recorder = new MediaRecorder();

         mCamera.unlock();
         recorder.setCamera(mCamera);

         recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
         recorder.setMaxDuration((int) 10000000); 
         recorder.setVideoSize(320, 240); 
         recorder.setVideoFrameRate(15); 
         recorder.setOutputFile(FULL_PATH_TO_LOCAL_FILE + counter + MP4);

         try{
             recorder.prepare();
         } catch (Exception e){
             finish();
         }

         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

         ticker = new Timer();
         locWriteTask = new WriteTimeLocationTimerTask(ll);
         ticker.schedule(locWriteTask, 0, DELAY);   

         recorder.start();
    }
like image 632
Alex A. Renoire Avatar asked Apr 13 '12 20:04

Alex A. Renoire


1 Answers

You may catch a RuntimeException at the MediaRecorder.stop() method.

Example:

MediaRecorder mRecorder = new MediaRecorder();
File mFile = new File("The output file's absolutePath");

... //config the mRecorder
mRecorder.setOutputFile(mFile.getAbsolutePath());

... //prepare() ...
mRecorder.start();

try {
    mRecorder.stop();
} catch(RuntimeException e) {
    mFile.delete();  //you must delete the outputfile when the recorder stop failed.
} finally {
    mRecorder.release();
    mRecorder = null;
}
like image 107
TaoZang Avatar answered Nov 15 '22 11:11

TaoZang