【Android】用MediaRecorder錄製視頻太短崩的問題,androidrecorder

來源:互聯網
上載者:User

【Android】用MediaRecorder錄製視頻太短崩的問題,androidrecorder


 

具體表現:

調用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);
    }

程式碼片段引自拍攝SDK Vitamio Recorder 2.0:http://www.cnblogs.com/over140/p/3704580.html

 

結尾吐槽:

1、這個bug太2了!!

2、Instagram你是怎麼發現的?! 

 

後續補充:

1、如果單獨設定不管用,請參考代碼部分的完整代碼設定。 


對於Android使用MediaRecorder錄影的問題

你解析度設定的多少?
 
Android MediaRecorder類調用問題,開發視頻監控系統的問題,Android系統中錄製視頻的方法

MeidaRecord要先申請空間,申請不到就錄不上
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.