Search code examples
androidmediarecorder

MediaRecoder.stop() take too long time


When I finished using MediaRecorder to record the video, the function stop() take me too long time to close the file stream,what happened to my code?

I have tried reset all the configs about the MediaRecorder,and I have checked the framework code about that ,the native function stop also takes too long time,but when I use the other Camera such as "OpenCamera", it take very short time to stop recording. Alse I checked out the Project "OpenCamera" code,and copy it's configs abount the MediaRecoder, it's not works for me.

this is my code about start and stop.

public void startRecord(String path) {
        if (currentState.compareTo(State.previewing) == 0) {
            this.mediaPath = path;
            currentState = State.recording;
            mediaRecorder = new MediaRecorder();
            mCamera.unlock();

            mediaRecorder.setCamera(mCamera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
            mediaRecorder.setVideoSize(videoSize.width, videoSize.height);
            mediaRecorder.setOrientationHint(videoRotation);

            mediaRecorder.setOutputFile(path);
            File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera");
            if (!file.exists()) {
                file.mkdirs();
            }
            try {
                mediaRecorder.prepare();
                mediaRecorder.start();
                audioManager.requestAudioFocus(audioChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
            } catch (Exception e) {
                LogTool.e(this, "error !!!", e);
                mCamera.lock();
                clearException(path);
                if (cameraCallback != null) {
                    cameraCallback.onRecordError();
                }
            }
        }
    }

    public void stopRecord(boolean force) {
        if (currentState.compareTo(State.recording) == 0) {
            audioManager.abandonAudioFocus(audioChangeListener);
            currentState = force ? State.previewing : State.showing;

            if (mediaRecorder != null) {
                try {
                    mediaRecorder.stop();
                    mediaRecorder.reset();
                    mediaRecorder.release();
                    mediaRecorder = null;
                } catch (Exception e) {
                    clearException(mediaPath);
                    if (!force) {
                        Toast.makeText(mContext, mContext.getResources().getString(R.string.video_tool_short),
                                Toast.LENGTH_SHORT).show();
                    }
                    if (cameraCallback != null) {
                        cameraCallback.onRecordError();
                    }
                    return;
                }

                if (force) {
                    File errorFile = new File(mediaPath);
                    if (errorFile.exists()) {
                        errorFile.delete();
                    }
                    if (cameraCallback != null) {
                        cameraCallback.onRecordError();
                    }
                } else {
                    if (cameraCallback != null) {
                        cameraCallback.onMediaSaved(mediaPath);
                    }
                }
            }
        }
    }

The log about the framework showed there:

01-01 13:32:08.650 4129-4129/? D/MediaRecorder: start begin
01-01 13:32:09.150 4129-4129/? D/MediaRecorder: start end
01-01 13:32:12.620 4129-4129/? D/MediaRecorder: stop start
01-01 13:32:17.770 4129-4129/? D/MediaRecorder: stop end

You can see the system log showed that it takes 5 sec to stop that.


Solution

  • It's maybe the device error. When I set setRecordingHint(true) , it's worked for me to solve the problem.