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