Android mediarecorder stop failed

18,195

Solution 1

If the recorder is not in a recording state, then the stop could fail.

See http://developer.android.com/reference/android/media/MediaRecorder.html

Solution 2

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;
}
Share:
18,195

Related videos on Youtube

Alex A. Renoire
Author by

Alex A. Renoire

Applied Math student, web developer.

Updated on June 04, 2022

Comments

  • Alex A. Renoire
    Alex A. Renoire almost 2 years

    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();
        }
    
  • Kalpesh
    Kalpesh over 9 years
    How i check that MediaRecorder is in a recording state ?
  • Bhargav
    Bhargav almost 8 years
    @Kalpesh The MediaRecorder api doesn't have provision to query its state unfortunately.
  • StarShine
    StarShine over 6 years
    what is this supposed to fix?
  • StarShine
    StarShine over 6 years
    adding this to what exactly?
  • CEO tech4lifeapps
    CEO tech4lifeapps about 6 years
    Add it to the class or activity that contains the rest of the code for your MediaRecorder
  • user924
    user924 almost 6 years
    well it always fails on Android 6.0.1, but works fine for 7.*. I use SURFACE as input
  • user924
    user924 almost 6 years
    @StarShine it's not related, useless answer