編輯:關於Android編程
具體表現:
調用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 資料整理,後續繼續補充,有需要的朋友可以參考下。
android快捷方式shortcut 管理
如下58同城快捷方式的效果: /** * 啟動某個activity是需要在manifest裡面定義 */ private void addShortCut
Android編程之OpenGL繪圖技巧總結
本文實例講述了Android編程之OpenGL繪圖技巧。分享給大家供大家參考,具體如下:很久不用OpenGL ES繪圖,怕自己忘記了,於是重新復習一遍,順便原理性的東西總
Android中微信搶紅包助手的實現詳解
實現原理通過利用AccessibilityService輔助服務,監測屏幕內容,如監聽狀態欄的信息,屏幕跳轉等,以此來實現自動拆紅包的功能。關於Accessibility
關於Android原生支持Gif動態圖的問題
大多人都說Android原生不支持Gif圖的動態展示,而我之前也是在這種影響下潛意識認為Android原生不支持Gif圖。但是我今天發現,其實WebView可以完美支持,