標籤:bar eth 作者 art span error ack mod isp
本文轉載自:http://blog.csdn.net/ouyang_peng/article/details/48048975
今天在調用MediaRecorder.stop(),報錯了,java.lang.RuntimeException: stop failed.
[html] view plain copy
- E/AndroidRuntime(7698): Cause by: java.lang.RuntimeException: stop failed.
- E/AndroidRuntime(7698): at android.media.MediaRecorder.stop(Native Method)
- E/AndroidRuntime(7698): at com.tintele.sos.VideoRecordService.stopRecord(VideoRecordService.java:298)
報錯代碼如下:
[java] view plain copy
- if (mediarecorder != null) {
- mediarecorder.stop();
- mediarecorder.release();
- mediarecorder = null;
- if (mCamera != null) {
- mCamera.release();
- mCamera = null;
- }
- }
stop()方法原始碼如下:
[java] view plain copy
- /**
- * Stops recording. Call this after start(). Once recording is stopped,
- * you will have to configure it again as if it has just been constructed.
- * Note that a RuntimeException is intentionally thrown to the
- * application, if no valid audio/video data has been received when stop()
- * is called. This happens if stop() is called immediately after
- * start(). The failure lets the application take action accordingly to
- * clean up the output file (delete the output file, for instance), since
- * the output file is not properly constructed when this happens.
- *
- * @throws IllegalStateException if it is called before start()
- */
- public native void stop() throws IllegalStateException;
原始碼中說了:Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start().The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.
現在,在mediarecorder.stop();這一句報錯了,現在在mediarecorder.stop();這句之前加幾句就不會報錯了
mediarecorder.setOnErrorListener(null);
mediarecorder.setOnInfoListener(null);
mediarecorder.setPreviewDisplay(null);
改後代碼如下:
[java] view plain copy
- if (mediarecorder != null) {
- //added by ouyang start
- try {
- //下面三個參數必須加,不加的話會奔潰,在mediarecorder.stop();
- //報錯為:RuntimeException:stop failed
- mediarecorder.setOnErrorListener(null);
- mediarecorder.setOnInfoListener(null);
- mediarecorder.setPreviewDisplay(null);
- mediarecorder.stop();
- } catch (IllegalStateException e) {
- // TODO: handle exception
- Log.i("Exception", Log.getStackTraceString(e));
- }catch (RuntimeException e) {
- // TODO: handle exception
- Log.i("Exception", Log.getStackTraceString(e));
- }catch (Exception e) {
- // TODO: handle exception
- Log.i("Exception", Log.getStackTraceString(e));
- }
- //added by ouyang end
-
- mediarecorder.release();
- mediarecorder = null;
- if (mCamera != null) {
- mCamera.release();
- mCamera = null;
- }
- }
====================================================================================
歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址:http://blog.csdn.net/ouyang_peng
我的Android進階之旅------>Android中MediaRecorder.stop()報錯 java.lang.RuntimeException: stop failed.【轉】