Android 用MediaRecorder录制视频太短崩的问题

具体表现:

调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。

错误信息:

     java.lang.RuntimeException: stop failed.
         at android.media.MediaRecorder.stop(Native Method)

解决办法:

在stop以前调用setOnErrorListener(null);就行了!

相关代码:

/** 开始录制 */
    @Override
    public MediaPart startRecord() {
        if (mMediaObject != null && mSurfaceHolder != null && !mRecording) {
            MediaPart result = mMediaObject.buildMediaPart(mCameraId, ".mp4");

            try {
                if (mMediaRecorder == null) {
                    mMediaRecorder = new MediaRecorder();
                    mMediaRecorder.setOnErrorListener(this);
                } else {
                    mMediaRecorder.reset();
                }

                // Step 1: Unlock and set camera to MediaRecorder
                camera.unlock();
                mMediaRecorder.setCamera(camera);
                mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

                // Step 2: Set sources
                mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//before setOutputFormat()
                mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//before setOutputFormat()

                mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

                //设置视频输出的格式和编码
                CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
                //                mMediaRecorder.setProfile(mProfile);
                mMediaRecorder.setVideoSize(640, 480);//after setVideoSource(),after setOutFormat()
                mMediaRecorder.setAudioEncodingBitRate(44100);
                if (mProfile.videoBitRate > 2 * 1024 * 1024)
                    mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 1024);
                else
                    mMediaRecorder.setVideoEncodingBitRate(mProfile.videoBitRate);
                mMediaRecorder.setVideoFrameRate(mProfile.videoFrameRate);//after setVideoSource(),after setOutFormat()

                mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//after setOutputFormat()
                mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//after setOutputFormat()

                //mMediaRecorder.setVideoEncodingBitRate(800);

                // Step 4: Set output file
                mMediaRecorder.setOutputFile(result.mediaPath);

                // Step 5: Set the preview output
                //                mMediaRecorder.setOrientationHint(90);//加了HTC的手机会有问题

                Log.e("Yixia", "OutputFile:" + result.mediaPath);

                mMediaRecorder.prepare();
                mMediaRecorder.start();
                mRecording = true;
                return result;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                Log.e("Yixia", "startRecord", e);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("Yixia", "startRecord", e);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Yixia", "startRecord", e);
            }
        }
        return null;
    }

    /** 停止录制 */
    @Override
    public void stopRecord() {
        long endTime = System.currentTimeMillis();
        if (mMediaRecorder != null) {
            //设置后不会崩
            mMediaRecorder.setOnErrorListener(null);
            mMediaRecorder.setPreviewDisplay(null);
            try {
                mMediaRecorder.stop();
            } catch (IllegalStateException e) {
                Log.w("Yixia", "stopRecord", e);
            } catch (RuntimeException e) {
                Log.w("Yixia", "stopRecord", e);
            } catch (Exception e) {
                Log.w("Yixia", "stopRecord", e);
            }
        }

        if (camera != null) {
            try {
                camera.lock();
            } catch (RuntimeException e) {
                Log.e("Yixia", "stopRecord", e);
            }
        }

        mRecording = false;
    }

    /** 释放资源 */
    @Override
    public void release() {
        super.release();
        if (mMediaRecorder != null) {
            mMediaRecorder.setOnErrorListener(null);
            try {
                mMediaRecorder.release();
            } catch (IllegalStateException e) {
                Log.w("Yixia", "stopRecord", e);
            } catch (Exception e) {
                Log.w("Yixia", "stopRecord", e);
            }
        }
        mMediaRecorder = null;
    }

    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        try {
            if (mr != null)
                mr.reset();
        } catch (IllegalStateException e) {
            Log.w("Yixia", "stopRecord", e);
        } catch (Exception e) {
            Log.w("Yixia", "stopRecord", e);
        }
        if (mOnErrorListener != null)
            mOnErrorListener.onVideoError(what, extra);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值