MediaRecorder.stop() hanging with Android 4.0 (ICS)

后端 未结 9 1193
夕颜
夕颜 2021-02-04 01:08

When calling stop() within my Video Capture activity, on occasion, the software will hang and will not come back to life. Only triggering an ANR by hitting \"Back\

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 01:49

    In my case, the reason this happened was that I stopped the camera prevew, changed the preview resolution, then restarted the preview all while recording. Something that would look like this if you compacted all of the method calls:

    camera.startPreview();
    camera.unlock();
    
    recorder = new MediaRecorder();
    recorder.setCamera(camera);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoFrameRate(24);
    recorder.setVideoEncodingBitRate(3000000);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    recorder.setOutputFile(output.getAbsolutePath());
    recorder.setPreviewDisplay(holder.getSurface());
    recorder.prepare();
    recorder.start();
    
    camera.stopPreview();
    Camera.Parameters cameraParameters = camera.getParameters();
    cameraParameters.setPreviewSize(x, y);
    camera.setParameters(cameraParameters);
    camera.startPreview();
    
    recorder.stop();
    recorder.reset();
    recorder.release();
    recorder = null;
    camera.lock();
    camera.stopPreview();
    

    In reality, the reason the preview size was being changed after the recording started is complicated and domain specific, so I will save the details. However, the point is the same: you cannot change the preview size without stopping it, but you cannot stop the preview after the media recorder has started. I'm sure there are other things that could cause this, like any other number of camera parameters.

    You should be sure to check that the camera is not disturbed while recording a video.

提交回复
热议问题