解決Android MediaRecorder錄制視頻過短問題
具體表現(xiàn):
調(diào)用MediaRecorder的start()與stop()間隔不能小于1秒(有時候大于1秒也崩),否則必崩。
錯誤信息:
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ī)會有問題 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è)置后不會崩 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 資料整理,后續(xù)繼續(xù)補(bǔ)充,有需要的朋友可以參考下。
相關(guān)文章
Flutter開發(fā)之Widget自定義總結(jié)
這篇文章主要給大家介紹了關(guān)于Flutter開發(fā)中Widget自定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Android CoordinatorLayout詳解及實(shí)例代碼
這篇文章主要介紹了Android CoordinatorLayout詳解及實(shí)例代碼的相關(guān)資料,CoordinatorLayout基本實(shí)現(xiàn)兩個功能: 作為頂層布局 和調(diào)度協(xié)調(diào)子布局,這里詳細(xì)介紹此部分知識,需要的朋友可以參考下2016-12-12Android?Canva實(shí)現(xiàn)漸變進(jìn)度條
這篇文章主要為大家介紹了Android?Canva實(shí)現(xiàn)漸變進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android實(shí)現(xiàn)可點(diǎn)擊的幸運(yùn)大轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可點(diǎn)擊的幸運(yùn)大轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02實(shí)時獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享
本文我們分享一個實(shí)時獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享,可以作為學(xué)習(xí)使用,本文貼出部分重要代碼,需要的朋友可以參考下本文2015-09-09Android仿QQ未讀消息--紅點(diǎn)拖拽刪除【源代碼】
本文Demo是一款仿qq未讀消息拖拽刪除的例子,繼承RelativeLayout的WaterDrop實(shí)現(xiàn)了圓形圖標(biāo)功能;繼承ImageView的CircleImageView圓形圖片功能。效果非常不錯,很適合有圓形設(shè)計的朋友參考2017-04-04