栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决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);
  }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/162070.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号