日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 用MediaRecorder录制视频太短崩的问题

發(fā)布時(shí)間:2024/4/15 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 用MediaRecorder录制视频太短崩的问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

具體表現(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)代碼:

/** 開(kāi)始錄制 */@Overridepublic 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 sourcesmMediaRecorder.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);elsemMediaRecorder.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ì)有問(wèn)題 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;}/** 停止錄制 */@Overridepublic 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;}/** 釋放資源 */@Overridepublic 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;}@Overridepublic 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);}

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhujiabin/p/5973334.html

超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的Android 用MediaRecorder录制视频太短崩的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。