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

北城以北 提交于 2019-12-03 08:29:38

问题


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" will allow me to kill the activity.

Within the log, I see the following line repeated over and over:

W/CameraSource(YYYYY): Timed out waiting for incoming camera video frames: XXXXXX us

Has anyone else seen this behavior? Any workarounds?


回答1:


We've been struggling for this issue for a long time too. We have just copied the code from android developer site for capturing video but the application hangs before mediarecorder.stop() is called. Upon debugging almost line by line, I found out the ff. line causes the issue:

captureButton.setText("start");

I have commented it out and stopping the mediarecorder will no longer cause an ANR. What I did was instead of changing the text of the button, I just change the background.

captureButton.setBackgroundResource(R.drawable.icon_post_camera_record_main);

Without seeing your code, I'm not sure if we have the same cause of the issue but this fixes mine. I'm still searching why this happens for settext() and not for setBackgroundResource. My guess is that it has something to do with background/async task but it's still a guess.




回答2:


This also happened to me because I was releasing the camera before performing stop() on the recorder. It also explains the error message "Timed out waiting for incoming camera video frames". It's waiting for a camera that is already released. Make sure to stop the recorder - and only then release the camera:

mMediaRecorder.stop();
mMediaRecorder.release();

camera.stopPreview();
camera.release();



回答3:


I'd recommand that you do it in a background thread, so that your app doesn't get stuck, even if the stop() method blocks:

    new Thread("STOP_RECORDER") {
        public void run() {
            Log.d(TAG, "Stopping recorder...");
            mediaRecorder.stop();
            Log.d(TAG, "Recorder successfully stopped");
        }
    }.start();



回答4:


I accidentally rejected an answer during review that was given using an 'edit' of the question:

user2171513 answered:

I was facing the same issue and the solution that I found was to add mCamera.lock(); right before mCamera.unlock(); before camera is set in MediaRecorder..




回答5:


check out this-

            @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        releaseMediaRecorder();
        releaseCamera();
    }
     private void releaseMediaRecorder(){
          if (recorder != null) {
              recorder.reset();   // clear recorder configuration
              recorder.release(); // release the recorder object
              recorder = null;
              camera.lock();           // lock camera for later use
          }
      }

      private void releaseCamera(){
          if (camera != null){
              camera.release();        // release the camera for other applications
              camera = null;
          }
      }
             public void stopRecording() { // Toast.makeText(this,"Inside the stop                   recording",Toast.LENGTH_SHORT).show();
        //  camera.unlock();
        recorder.stop();
                // recorder.reset();
        recorder.release();#release the recorder after stop important.

    }



回答6:


I also got a error like this. My activity contains a mutative SurfaceView that display preview,so after I start recording, the width of SurfaceView shrinked,and I could not stop the recording, get the ANR error。 I sovle the problem by fix the SurfaceView's dimension。 Hope it helpful




回答7:


Probably you're starting the preview and media recorder init in onCreate(). Move this to a later point or add new Handler.postDelayed(runnable, 1000);




回答8:


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.




回答9:


In my case I been trying to stop recording on Activity.onStop, when I moved the call to MediaRecorder.stop to Activity.onPause it fixed my problem.



来源:https://stackoverflow.com/questions/8914454/mediarecorder-stop-hanging-with-android-4-0-ics

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