博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Android】用MediaRecorder录制视频太短崩的问题
阅读量:6983 次
发布时间:2019-06-27

本文共 3741 字,大约阅读时间需要 12 分钟。

具体表现:

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

代码片段引自拍摄SDK Vitamio Recorder 2.0:http://www.cnblogs.com/over140/p/3704580.html

 

结尾吐槽:

1、这个bug太2了!!

2、Instagram你是怎么发现的?! 

 

后续补充:

1、如果单独设置不管用,请参考代码部分的完整代码设置。 

转载:http://www.cnblogs.com/over140/p/3811084.html

你可能感兴趣的文章
新书上市:《FLUENT 14.0超级学习手册》
查看>>
mysql数据库query cache
查看>>
使用docker commit 来扩展一个image
查看>>
jsp 防止sql注入 之 preparestatement篇(转载)
查看>>
Linux之Ansible入门用法(实验解析)
查看>>
Linux系统如何在开机时修改root密码
查看>>
共济失调对我们的危害你知道吗
查看>>
Anychat的绝对路径与相对路径
查看>>
我的友情链接
查看>>
如何使用网络库实现应用级消息收发
查看>>
Single Area OSPF
查看>>
rhel6之yum
查看>>
selenium+ant+testng测试框架简单介绍
查看>>
自己写的DBUtil数据库连接工具类
查看>>
登录多实例MySQL失败,修改密码临时解决,原因不明
查看>>
SCCM 2007 R2部署、操作详解系列之部署篇
查看>>
hystrix thread pool Metrics
查看>>
MDT2012部署问题,MDT中的驱动是如何工作的
查看>>
注意;
查看>>
Selenium 使用要点记录<二>
查看>>