本文转载自:http://blog.csdn.net/ouyang_peng/article/details/48048975

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

  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)

报错代码如下:

  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()方法源代码如下:

  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);

改后代码如下:

  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. mediarecorder.release();
  22. mediarecorder = null;
  23. if (mCamera != null) {
  24. mCamera.release();
  25. mCamera = null;
  26. }
  27. }
                ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

我的Android进阶之旅------>Android中MediaRecorder.stop()报错 java.lang.RuntimeException: stop failed.【转】的更多相关文章

  1. Android Studio 首次安装报错 Java.lang.RuntimeException:java.lang.NullPointerException...错

    下次安装报:Java.lang.RuntimeException: java.lang.NullPointerException......错 只需在文件..\Android Studio\bin\i ...

  2. Eclipse中启动tomcat报错java.lang.OutOfMemoryError: PermGen space的解决方法

    有的项目引用了太多的jar包,或者反射生成了太多的类,异或有太多的常量池,就有可能会报java.lang.OutOfMemoryError: PermGen space的错误, 我们知道可以通过jvm ...

  3. myeclipse的web project中使用dom4j报错java.lang.ClassNotFoundException: org.dom4j.Document

    在学习微信公众平台开发的过程中,参考了微信公众平台java开发详解(工程代码+解析)中的xml解析方法,是使用dom4j来解析的. 对于java中解析xml的方法,又参考了java解析xml的几种方式 ...

  4. myeclipse中运行tomcat报错java.lang.NoClassDefFoundError

    有关myeclipse的小问题,在myeclipse中运行tomcat时显示已启动,但是无法访问localhost:8080/,显示404错误.在控制台中发现报错代码如下: java.lang.NoC ...

  5. eclipse中启动tomcat报错 java.lang.ClassNotFoundException

    之前启动还好好的,某次启动tomcat就莫名其妙的报了这个java.lang.ClassNotFoundException的错.   检查maven依赖包,发现这个类是存在的. 然后一通clean操作 ...

  6. 我的Android进阶之旅------>解决Bug:打开eclipse报错,发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。

    今天来打开Eclipse 报错了,错误信息如下: [2015-08-01 09:07:43 - Android SDK] Error when loading the SDK: Error: Erro ...

  7. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  8. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  9. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

随机推荐

  1. #NOIP前数学知识总结

    我好菜啊…… 欧拉函数 欧拉函数φ(n),是小于n且和n互质的正整数(包括1)的个数. 性质: 1.对于质数n: φ(n)=n-1 2..对于n=pk φ(n)=(p-1)*pk-1 3.积性函数的性 ...

  2. Analysis of container and Injection in Java, their history and future.

    Container: 发展历程: 2000 年的时候 FreeBSD 开发了一个类似于 chroot 的容器技术 Jails,这是最早期,也是功能最多的容器技术.Jails 英译过来是监狱的意思,这个 ...

  3. js读写txt文件

    view plain<script language="javascript" type="text/javascript"> //读文件funct ...

  4. cc.Node—Action

    1: Action类是动作命令,我们创建Action,然后节点运行action就能够执行Action的动作; 2: Action分为两类: (1) 瞬时就完成的ActionInstant, (2) 要 ...

  5. Game Rank(NCPC 2016 大模拟)

    题目: The gaming company Sandstorm is developing an online two player game. You have been asked to imp ...

  6. 经典书籍---MySQL经典书籍下载

    以下是一些经典的MySQL书籍电子版,括号内为提取码,若需自取. 欢迎阅读纸质版,尊重作者版权 高性能MySQL_中文版 [ hre3 ] 高性能MySQL_英文版[ m2xj ] MySQL技术内幕 ...

  7. python爬虫30 | scrapy后续,把「糗事百科」的段子爬下来然后存到数据库中

    上回我们说到 python爬虫29 | 使用scrapy爬取糗事百科的例子,告诉你它有多厉害! WOW!! scrapy awesome!! 怎么会有这么牛逼的框架 wow!! awesome!! 用 ...

  8. [luoguP2285] [HNOI2004]打鼹鼠(DP)

    传送门 设f[i]表示i个鼹鼠出现后,打死鼹鼠的最大值 动态转移方程:f[i]=max{f[j]+1}, 条件:abs(x[i]-x[j])+abs(y[i]-y[j])<=time[i]-ti ...

  9. Minimal string 栈 贪心

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  10. [bzoj1577][Usaco2009 Feb]庙会捷运Fair Shuttle_贪心_线段树

    庙会捷运 Fair Shuttle bzoj-1577 Usaco-2009 Feb 题目大意:有一辆公交车从1走到n.有m群奶牛从$S_i$到$E_i$,第i群奶牛有$W_i$只.车有一个容量c.问 ...