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);
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

本文转自博客园农民伯伯的博客,原文链接: 【Android】用MediaRecorder录制视频太短崩的问题 ,如需转载请自行联系原博主。

Android播放器MediaPlayer与MediaRecorder:录制音频并播放 以下以使用Android的MediaPlayer和MediaRecorder录制音频为例加以说明: (1)Android MediaPlayer 本例以Android MediaPlayer播放Android音频资源为说明。
Android使用MediaRecorder类进行视频的录制。 需要注意,使用MediaRecorder 录音录像 的设置代码步骤一定要按照API指定的顺序来设置,否则报错 1、设置视频源,音频源,即输入源 2、设置输出格式 3、设置音视频的编码格式 一、首先看布局文件,...