MediaRecorder freezes the UI thread

最后都变了- 提交于 2019-12-24 11:20:07

问题


I'm using CameraKit library to record video in my app. The issue that I'm facing is that when i start recording then i need to show recording progress as well. But as i call video recording method in which MediaRecorder is prepared and started that freezes the UI thread for ~500-700ms which doesn't let progressBar animate and it feels like on long press of recording button is taking long time but infact it is MediaRecorder that is taking time. Can MediaRecorder be started in background thread without freezing the animation?

Following is the method, I tried Handler as well, but it didn't make much difference:

void captureVideo(final File videoFile,final int maxDuration,final VideoCapturedCallback callback) {
        Log.d("usm_recording_captureVideo","init");
        synchronized (mCameraLock)
        {
               /*new Handler().post(new Runnable() {
                    @Override
                    public void run() {*/
                        try {
                            Log.d("usm_recording_captureVideo", "try");

                            if (prepareMediaRecorder(videoFile, maxDuration)) {
                                Log.d("usm_recording_captureVideo", "prepareMediaRecorder");

                                mMediaRecorder.start();
                                mRecording = true;
                                Log.d("usm_recording_captureVideo", "started recorder");


                                Camera1.this.mVideoCallback = callback;

                            } else {
                                releaseMediaRecorder();
                            }
                        }
                        catch (Exception e) {
                            Log.d("asad_capture_video", "e : " + e.getMessage());
                            releaseMediaRecorder();
                        }
                    /*}
                });*/
            Log.d("usm_recording_captureVideo","captureVideo");
        }

    }

Edit:

I've used a HandlerThread to start the Media recorder which doesn't let UI freeze now, But as progress starts even before waiting for MediaRecorder to be started. So, actual saved video is of ~500-700ms less length than expected recording.

HandlerThread thread;
    @Override
    void captureVideo(final File videoFile,final int maxDuration,final VideoCapturedCallback callback) {
        Log.d("usm_recording_captureVideo","init");
        synchronized (mCameraLock)
        {
                                thread = new HandlerThread("MediaRecorderHandlerThread");
                                thread.start();

                                Handler handler = new Handler(thread.getLooper());
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                           // Log.d("usm_recording_captureVideo", "try");

                                            if (prepareMediaRecorder(videoFile, maxDuration)) {
                                           // Log.d("usm_recording_captureVideo", "prepareMediaRecorder");

                                            mMediaRecorder.start();
                                        } else {
                                            releaseMediaRecorder();
                                        }
                                            mRecording = true;
                                          //  Log.d("usm_recording_captureVideo", "started recorder");


                                            Camera1.this.mVideoCallback = callback;

                                        }
                                        catch (Exception e) {
                                          //  Log.d("asad_capture_video", "e : " + e.getMessage());
                                            releaseMediaRecorder();
                                        }

                                    }
                                });
            Log.d("usm_recording_captureVideo","captureVideo");
        }

    }

来源:https://stackoverflow.com/questions/58061741/mediarecorder-freezes-the-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!