Java.lang.RuntimeException: stop failed

版权声明:本文为【欧阳鹏】原创文章,欢迎转载,转载请注明出处! 【http://blog.csdn.net/ouyang_peng】

今天在调用MediaRecorder.stop(),报错了,Java.lang.RuntimeException: stop failed.

 

[html] view plain copy

  1. E/AndroidRuntime(7698): Cause by: java.lang.RuntimeException: stop failed.  
  2. E/AndroidRuntime(7698):            at android.media.MediaRecorder.stop(Native Method)  
  3. E/AndroidRuntime(7698):            at com.tintele.sos.VideoRecordService.stopRecord(VideoRecordService.java:298)  


 

 

报错代码如下:

 

[java] view plain copy

  1. if (mediarecorder != null) {  
  2.         mediarecorder.stop();  
  3.         mediarecorder.release();  
  4.         mediarecorder = null;  
  5.         if (mCamera != null) {  
  6.             mCamera.release();  
  7.             mCamera = null;  
  8.         }  
  9.     }  

 

stop()方法源代码如下:

 

[java] view plain copy

  1. /** 
  2.      * Stops recording. Call this after start(). Once recording is stopped, 
  3.      * you will have to configure it again as if it has just been constructed. 
  4.      * Note that a RuntimeException is intentionally thrown to the 
  5.      * application, if no valid audio/video data has been received when stop() 
  6.      * is called. This happens if stop() is called immediately after 
  7.      * start(). The failure lets the application take action accordingly to 
  8.      * clean up the output file (delete the output file, for instance), since 
  9.      * the output file is not properly constructed when this happens. 
  10.      * 
  11.      * @throws IllegalStateException if it is called before start() 
  12.      */  
  13.     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().

 

 

现在,在mediarecorder.stop();这一句报错了,现在在mediarecorder.stop();这句之前加几句就不会报错了

mediarecorder.setOnErrorListener(null);
mediarecorder.setOnInfoListener(null);  
mediarecorder.setPreviewDisplay(null);

改后代码如下:

 

[java] view plain copy

  1. if (mediarecorder != null) {  
  2.             //added by ouyang start  
  3.             try {  
  4.                 //下面三个参数必须加,不加的话会奔溃,在mediarecorder.stop();  
  5.                 //报错为:RuntimeException:stop failed  
  6.                 mediarecorder.setOnErrorListener(null);  
  7.                 mediarecorder.setOnInfoListener(null);    
  8.                 mediarecorder.setPreviewDisplay(null);  
  9.                 mediarecorder.stop();  
  10.             } catch (IllegalStateException e) {  
  11.                 // TODO: handle exception  
  12.                 Log.i("Exception", Log.getStackTraceString(e));  
  13.             }catch (RuntimeException e) {  
  14.                 // TODO: handle exception  
  15.                 Log.i("Exception", Log.getStackTraceString(e));  
  16.             }catch (Exception e) {  
  17.                 // TODO: handle exception  
  18.                 Log.i("Exception", Log.getStackTraceString(e));  
  19.             }  
  20.             //added by ouyang end  
  21.               
  22.             mediarecorder.release();  
  23.             mediarecorder = null;  
  24.             if (mCamera != null) {  
  25.                 mCamera.release();  
  26.                 mCamera = null;  
  27.             }  
  28.         }  
  29.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值