解決Android MediaRecorder錄制視頻過短問題
具體表現(xiàn):
調(diào)用MediaRecorder的start()與stop()間隔不能小于1秒(有時(shí)候大于1秒也崩),否則必崩。
錯(cuò)誤信息:
java.lang.RuntimeException: stop failed. at android.media.MediaRecorder.stop(Native Method)
解決辦法:
在stop以前調(diào)用setOnErrorListener(null);就行了!
相關(guān)代碼:
/** 開始錄制 */
@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);
//設(shè)置視頻輸出的格式和編碼
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的手機(jī)會(huì)有問題
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) {
//設(shè)置后不會(huì)崩
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);
}
以上就是對(duì)Android MediaRecorder 資料整理,后續(xù)繼續(xù)補(bǔ)充,有需要的朋友可以參考下。
相關(guān)文章
詳解Android控件之DatePicker、TimePicker探究
本篇文章主要介紹了Android控件之DatePicker、TimePicker探究,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置
這篇文章主要介紹了Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android 自定義圓形帶刻度漸變色的進(jìn)度條樣式實(shí)例代碼
這篇文章主要介紹了Android 自定義圓形帶刻度漸變色的進(jìn)度條的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android自定義豎直方向SeekBar多色進(jìn)度條
這篇文章主要介紹了Android自定義SeekBar實(shí)現(xiàn)多色豎直進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android自定義View仿IOS圓盤時(shí)間選擇器
這篇文章主要介紹了Android自定義View仿IOS圓盤時(shí)間選擇器,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
三款A(yù)ndroid炫酷Loading動(dòng)畫組件推薦
這篇文章主要介紹了三款A(yù)ndroid炫酷Loading動(dòng)畫組件推薦,本文介紹了CircleProgress、android-shapeLoadingView、WaitingDots等三款Loading組件,并給出了運(yùn)行效果圖,需要的朋友可以參考下2015-05-05
Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android實(shí)現(xiàn)仿微軟系統(tǒng)加載動(dòng)畫效果
這篇文章主要介紹了Android實(shí)現(xiàn)仿微軟系統(tǒng)加載動(dòng)畫效果的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04

