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

來源:互聯網
上載者:User

標籤:

今天在調用MediaRecorder.stop(),報錯了,java.lang.RuntimeException: stop failed.

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)



報錯代碼如下:

if (mediarecorder != null) {mediarecorder.stop();mediarecorder.release();mediarecorder = null;if (mCamera != null) {mCamera.release();mCamera = null;}}


stop()方法原始碼如下:

/**     * 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().


現在,在mediarecorder.stop();這一句報錯了,現在在mediarecorder.stop();這句之前加幾句就不會報錯了

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

改後代碼如下:

if (mediarecorder != null) {//added by ouyang starttry {//下面三個參數必須加,不加的話會奔潰,在mediarecorder.stop();//報錯為:RuntimeException:stop failedmediarecorder.setOnErrorListener(null);mediarecorder.setOnInfoListener(null);  mediarecorder.setPreviewDisplay(null);mediarecorder.stop();} catch (IllegalStateException e) {// TODO: handle exceptionLog.i("Exception", Log.getStackTraceString(e));}catch (RuntimeException e) {// TODO: handle exceptionLog.i("Exception", Log.getStackTraceString(e));}catch (Exception e) {// TODO: handle exceptionLog.i("Exception", Log.getStackTraceString(e));}//added by ouyang endmediarecorder.release();mediarecorder = null;if (mCamera != null) {mCamera.release();mCamera = null;}}



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

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

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

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



著作權聲明:本文為歐陽鵬原創文章,歡迎轉載,轉載請註明出處http://blog.csdn.net/ouyang_peng

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.