解决Android MediaRecorder录制视频过短问题

所属分类: 软件编程 / Android 阅读数: 1992
收藏 0 赞 0 分享

具体表现:

  调用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);
  }

以上就是对Android MediaRecorder 资料整理,后续继续补充,有需要的朋友可以参考下。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多