[Android] androidrecorder
Specific performance:
Call MediaRecorder's start () and stop () interval cannot be less than 1 second (sometimes more than 1 second also collapse), otherwise it will collapse.
Error message:
Java. lang. RuntimeException: stop failed.
At android. media. MediaRecorder. stop (Native Method)
Solution:
Call setOnErrorListener (null); before stop!
Related code:
/** Start recording */
@ 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 );
// Sets the video output format and encoding.
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); // a problem may occur when an HTC mobile phone is added.
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;
}
/** Stop recording */
@ Override
Public void stopRecord (){
Long endTime = System. currentTimeMillis ();
If (mMediaRecorder! = Null ){
// Does not crash after setting
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;
}
/** Release resources */
@ 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 );
}
Code snippets reference shot SDK Vitamio Recorder 2.0: http://www.cnblogs.com/over140/p/3704580.html
At the end:
1. This bug is too high !!
2. How did you find Instagram ?!
Subsequent supplements:
1. If the settings are not used, refer to the complete code settings in the Code section.
MediaRecorder video recording for Android
What is your resolution set?
Android MediaRecorder class call problems, video monitoring system development problems, Android system video recording methods
MeidaRecord must first apply for a space, and cannot be recorded if the application fails.