`

Media start error原因分析及解决方法

阅读更多

之前在项目的时候,遇到到Media Recorder在快速启动停止等一系列操作的后,再次启动时,会failed的问题,类似的Log如下:

 

ERROR/MediaRecorder(9008): start failed: -19                                                                            
WARN/dalvikvm(9008): threadid=15: thread exiting with uncaught exception (group=0x40ad01f8)                             
ERROR/AndroidRuntime(9008): FATAL EXCEPTION: Thread-337                                                                 
ERROR/AndroidRuntime(9008): java.lang.RuntimeException: start failed.                                                   
ERROR/AndroidRuntime(9008):     at android.media.MediaRecorder.start(Native Method)                                     
ERROR/AndroidRuntime(9008):     at com.huawei.ca.eivs.camera.AndroidVideo.initMediaRecoder(AndroidVideo.java:429)       
ERROR/AndroidRuntime(9008):     at com.huawei.ca.eivs.camera.AndroidVideo.sendLiveToMdu(AndroidVideo.java:511)          
ERROR/AndroidRuntime(9008):     at com.huawei.ca.eivs.camera.AndroidVideo$ListenInviteThread.run(AndroidVideo.java:241) 
WARN/ActivityManager(210):   Force finishing activity com.huawei.ca.eivs.ui/com.huawei.ca.eivs.camera.AndroidVideo      
ERROR/ActivityManager(210): exception bw.write()java.io.IOException: Transport endpoint is not connected                

 

这个时候,退出自己的软件,打开系统相机,会提示“相机故障,无法连接到相机”,只能重启机器才能正常使用系相机。

 

根据反复测试抓log发现,造成MediaRecorder start失败的原因是:在程序中反复启动停止MediaRecord时,存在在无效的状态上stop MediaRecorder的情况,而且没有捕获此操作引发的异常,没有对MediaRecorder进行reset、release造成的。

 

具体的MediaRecorder状态迁移,请参看SDK。 

 

解决办法:
解决这个问题的关键是保证MediaRecorder能在合适的状态执行合适的动作。


下面是我的解决方法,希望能给遇到这个问题的人一个灵感:

 

private void releaseMediaRecorder()
    {
        if (mMediaRecorder != null)
        {
            // 内部标识是否正在录像的变量,如果不需要可以去掉
            if (isRecord)
            {
                try
                {
                    mMediaRecorder.setOnErrorListener(null);
                    mMediaRecorder.setOnInfoListener(null);
                    // 停止
                    mMediaRecorder.stop();
                   
                }
                catch (RuntimeException e)
                {
                    e.printStackTrace();
                   
                    // 如果发生异常,很可能是在不合适的状态执行了stop操作
                    // 所以等待一会儿
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e1)
                    {
                        Log.e(TAG, "sleep for second stop error!!");
                    }
                }
                isRecord = false;
            }
           
            // 再次尝试停止MediaRecorder
            try
            {
                mMediaRecorder.stop();
            }
            catch (Exception e)
            {
                Log.e(TAG, "stop fail2", e);
            }
           
            // 等待,让停止彻底执行完毕
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e1)
            {
                Log.e("TAG", "sleep for reset error Error", e1);
            }
           
            // 然后再进行reset、release
            mMediaRecorder.reset();
            mMediaRecorder.release();
           
            mMediaRecorder = null;
        }
    }

 

这是我经历痛苦后的总结,希望能对遇到类似问题的同伴有点帮助~

分享到:
评论
2 楼 diyangxia 2014-04-15  

看起来比较靠谱
1 楼 whhpc19891120 2013-04-25  
大哥,还是用了你的方法问题还是存在

相关推荐

    MediaEx.zip_The Exception

    try { // create a player to play the media specified in the ...mediaPlayer.start() // start playing the media clip } catch (Exception iOException) { System.err.println("Error reading from the source") }

    GetWebPics专业版2.9

    You can start downloading pictures (or other media content) quickly, simply after inputting the address of the first page with gallery. Smart page analyzer will follow every link on the page or script...

    hls.min.js

    var i={NETWORK_ERROR:"networkError",MEDIA_ERROR:"mediaError",MUX_ERROR:"muxError",OTHER_ERROR:"otherError"},a={MANIFEST_LOAD_ERROR:"manifestLoadError",MANIFEST_LOAD_TIMEOUT:"manifestLoadTimeOut",...

    BURNINTEST--硬件检测工具

    - 32-bit BurnInTest PRO 5.3.1020 would not start on Windows 2000. This has been corrected. Release 5.3 build 1020 WIN32 release 29 May 2008 - BurnInTest could have crashed on accessing bad video ...

    UMP Pro Win Mac Linux WebGL 2.0.3(u5.3.0).txt

    UMP is universal Media Framework plugin for Unity that based on Video LAN Codec (libVLC) native libraries. Support E-mail: unitydirectionkit@gmail.com Try: Demo (Win64) Demo (Mac64) Important: - ...

    一个不错的地形编辑器源码.zip

    strcat (PreviewDir, "media\\levels\\"); _makepath (DestBspName, PreviewDrive, PreviewDir, Name, Ext); */ // make dest path same as source path strcpy(DestBspName, PreviewFilename); } // if ...

    C#全能速查宝典

    《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...

    Overview of the MPEG-4 Standard

    Scope and features of the MPEG-4 standard m 1.1 Coded representation of media objects m 1.2 Composition of media objects m 1.3 Description and synchronization of streaming data for media ...

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    正在分析软件包的依赖关系树 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libfakeroot Use 'apt-...

    包含拍照截图修图的安卓程序

    Toast.makeText(MainActivity.this, "ActivityResult resultCode error", Toast.LENGTH_SHORT).show(); return; } switch(requestCode) { case 1: Log.d(TAG, "onActivityResult,1,"+outputImage...

    SD卡修复软件(松下出品)

    the Software will be uninterrupted or error free. Matsushita will not be liable for any damage suffered by Licensee arising from or in connection with Licensee's use of the Software. Copyright: ...

    adb1.0.26包含fastboot.exe

    常见安装失败输出代码、含义及可能的解决办法如下: 输出 含义 解决办法 INSTALL_FAILED_ALREADY_EXISTS 应用已经存在,或卸载了但没卸载干净 adb install 时使用 -r 参数,或者先 adb uninstall <packagename> 再...

    thl_r16_tinav2.0_hm1375验证通过_增加打印设备ID_20170824_1447.7z

    R:\wyb\thl_r16_tinav2.0_hm1375\lichee\linux-3.4\drivers\media\video\sunxi-vfe\device\hm1375.c 2、 R:\wyb\thl_r16_tinav2.0_hm1375\lichee\linux-3.4\drivers\media\video\sunxi-vfe\device\Makefile ...

    hm1375_tinav2.1验证通过_增加设备ID的读取显示_20170825_1333没有外层目录.7z

    R:\wyb\thl_r16_tinav2.0_hm1375\lichee\linux-3.4\drivers\media\video\sunxi-vfe\device\hm1375.c 2¡¢ R:\wyb\thl_r16_tinav2.0_hm1375\lichee\linux-3.4\drivers\media\video\sunxi-vfe\device\Makefile EXTRA...

    DOS下用的GHOST 11.0.2.1573 版本(很好用的哦)

    Error Count (bad media) 0 Error Count (other) 5874 Last Error Win32 error: (0x00000037) The specified network resource or device is no longer available. Remote Drives AsyncIo : 0 Image Devices ...

    android 多线程多任务断点下载

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File dir = Environment.getExternalStorageDirectory();//文件保存目录 System.out.println("文件保存目录=="+dir);...

    Android2.0源代码

    public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub return false; } //Video播完的时候得到通知 @Override public void onCompletion(MediaPlayer mp) { ...

    a20_hummingbird_v4.5_v1.0_csi01_2ov7670_rtl8188eus 20150918 1830 JNI.7z

    E/WifiConfigStore( 1670): Error parsing configurationjava.io.FileNotFoundException: /data/misc/wifi/ipconfig.txt: open failed: ENOENT (No such file or directory) D/BluetoothAdapter( 1931): 1103979928:...

    simplewebrtc.js webrtc网页视频开发帮助工具

    error: function (){} }; // normalize environment var RTCPeerConnection = null, getUserMedia = null, attachMediaStream = null, reattachMediaStream = null, browser = null, webRTCSupport = true; ...

    BUS Hound

    In the event the system is restarted, it will start capturing data early in the system startup process. <br>If Bus Hound is exited while stopped, no capture will occur until returning to the ...

Global site tag (gtag.js) - Google Analytics