IllegalStateException异常与MediaRecorder.start():空 [英] IllegalStateException with MediaRecorder.start() : null

查看:2183
本文介绍了IllegalStateException异常与MediaRecorder.start():空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的视频记录,这里是我的code,以便我打电话吧:

I'm trying to create a simple video recorder and here's my code in order that I'm calling it:

protected boolean prepareForVideoRecording() {
        try {
            mCamera.unlock();
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setCamera(mCamera);
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
            mMediaRecorder.setOutputFile(getOutputMediaFile1(MEDIA_TYPE_VIDEO).toString());
            mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
            try {
                mMediaRecorder.prepare();
                Toast.makeText(getContextOfMainFragment(), "Prepared!", Toast.LENGTH_SHORT).show();
            } catch (IllegalStateException e) {
                Log.e(TAG,
                        "IllegalStateException when preparing MediaRecorder "
                                + e.getMessage());
                e.getStackTrace();
                releaseMediaRecorder();
                return false;
            } catch (IOException e) {
                Log.e(TAG,
                        "IOException when preparing MediaRecorder "
                                + e.getMessage());
                e.getStackTrace();
                releaseMediaRecorder();
                return false;
            }
            return true;

        } catch (Exception e) {
            return false;
        }
    }

请忽略敬酒的消息,我有点有时使用它们进行调试。接下来,唯一剩下的就是调用mMediaRecorder.start()
我这样做是点击一个按钮。事实上,我已经添加了2个按钮,1按钮调用该上述方法,其他的按钮可以使开始通话。我敢肯定,这同样code工作了我一次,我不知道为什么没有工作了。

Please ignore the toast messages, I kinda use them sometimes for debugging. Next, the only thing is left is calling mMediaRecorder.start() I do this on a button click. In fact, I've added 2 buttons, 1st button calls this above method, the other button makes the start call. I'm sure this same code worked for me once, I don't know why isn't it working anymore.

我说在我的mMediaRecorder.start(一个尝试catch)与一个IllegalStateException打电话,这里就是我的了:

I added a try catch around my mMediaRecorder.start() call with an IllegalStateException and here's what I got:

04-09 10:38:18.350: E/MediaRecorder(29678): start failed: -38
04-09 10:38:18.350: E/VideoTut_Main(29678): Illegal state exception with MediaRecorder.Start() :  null

感谢您的帮助! :)

Thanks for your help! :)

推荐答案

我已经写在我的关于这样的问题,博客。总结它有(至少据我所知)三种方法来设置Android mediarecorder和每个方法我已经在我的手里有一个装置,其中该方法没有奏效。所以,你需要用户尝试哪种方法他的设备上。该方法如下:

I have written a lengthy article in my blog about problems like this. Summarizing it there are (at least to my knowledge) three methods to set up the Android mediarecorder and for each method I have had a device in my hands where this method did not work. So you need the user to try out which method works on his device. The methods are as follows:


  1. 使用视频大小,preVIEW大小等,但设置视频大小的设备默认设置明确的媒体记录到preVIEW的大小,你以后<从相机的参数获取/ STRONG>您已经启动了preVIEW窗口,但之前你解开相机记录。

  2. 使用摄像机的配置文件像你在code一样。它应该上有Android 4.0及以上版本的设备最完美的工作。我建议设置最大文件大小和视频长度虽然,但怀疑这是与Nexus 5的问题,我知道至少有一个其他设备(的Xperia射线升级到最新的固件版本),其中,这并不正常工作。

  3. 使用您从 getSupportedVideoSizes得到()从相机的参数。
  4. 明确的视频尺寸之一
  1. Use the devices default settings for video size, preview size etc., but set video size explicitly in the media recorder to the size of the preview that you get from the camera parameters after you have started the preview window but before you unlock the camera for recording.
  2. Use camcorder profiles like you did in your code. It should work flawlessly on most devices that have an Android version 4.0 and above. I would recommend to set maximum file size and video length though, but doubt that this is the problem with the Nexus 5. And I know at least one other device (Xperia ray upgraded to latest firmware version) where this does not work as well.
  3. Use one of the explicit video sizes that you get from getSupportedVideoSizes() from the camera parameters.

方法1和3。需要设置输出格式,音频连接codeR和视频连接codeR明确的,我总是用 MediaRecorder.OutputFormat.THREE_GPP MediaRecorder.AudioEn coder.AMR_NB MediaRecorder.VideoEn coder.MPEG_4_SP

Method 1. and 3. require setting the output format, audio encoder and video encoder explicitly for which I always use MediaRecorder.OutputFormat.THREE_GPP , MediaRecorder.AudioEncoder.AMR_NB and MediaRecorder.VideoEncoder.MPEG_4_SP.

我有一个小的免费应用程序称为视频定时在谷歌播放已经实现了所有三种方法让你可以尝试在Nexus 5的轻松的作品。

I have a small free app called Video Timer in Google Play which has implemented all three methods so you can try out easily what works on the Nexus 5.

这篇关于IllegalStateException异常与MediaRecorder.start():空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆