我的Android進階之旅------>Android中MediaRecorder.stop()報錯 java.lang.RuntimeException: stop failed.【轉】


本文轉載自:http://blog.csdn.net/ouyang_peng/article/details/48048975

今天在調用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().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
 
  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.         }  

 

 

 

                ====================================================================================

  作者:歐陽鵬  歡迎轉載,與人分享是進步的源泉!

  轉載請保留原文地址:http://blog.csdn.net/ouyang_peng


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM