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

作者:lqh 时间:2023-04-24 01:47:56 

具体表现:

  调用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
0
投稿

猜你喜欢

  • C#词法分析器之正则表达式的使用

    2023-06-21 13:10:58
  • java Apache poi 对word doc文件进行读写操作

    2023-09-23 02:13:15
  • java之swing下拉菜单实现方法

    2023-07-12 04:55:30
  • Android ListView万能适配器实例代码

    2022-03-06 02:19:50
  • servlet之session简介_动力节点Java学院整理

    2023-07-07 00:51:07
  • SpringCloudConfig之client端报错Could not resolve placeholder问题

    2023-11-23 11:19:17
  • SpringMVC中事务是否可以加在Controller层的问题

    2021-12-09 01:20:15
  • Android Mms之:对话与联系人关联的总结详解

    2023-12-06 13:12:57
  • Java 继承与多态的深入理解

    2023-10-05 04:25:41
  • Java设计模式之抽象工厂模式浅析讲解

    2022-08-08 18:26:47
  • Java对象数组定义与用法详解

    2021-11-01 08:33:18
  • Java中的notyfy()和notifyAll()的本质区别

    2022-06-05 22:46:19
  • Spring MVC Mybatis多数据源的使用实例解析

    2022-02-13 20:37:19
  • 详解Spring Boot中使用AOP统一处理Web请求日志

    2021-08-24 15:50:17
  • Java中switch的三种用法方式小结

    2023-11-24 03:40:21
  • Flutter Widget 之package mason实现详解

    2021-07-08 23:43:08
  • Javaweb基础入门requse原理与使用

    2021-10-07 20:55:29
  • JAVA IDEA入门使用手册(新手小白必备)

    2022-10-21 16:31:25
  • Flutter WillPopScope拦截返回事件原理示例详解

    2023-07-19 12:53:35
  • Springmvc ResponseBody响应json数据实现过程

    2022-06-12 15:22:30
  • asp之家 软件编程 m.aspxhome.com