MediaRecorder.stop() hanging with Android 4.0 (ICS)
Asked Answered
S

9

22

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?

Suspensory answered 18/1, 2012 at 17:23 Comment(1)
could you provide some code of your "Video Capture activity"? especially where method stop() get calledBold
L
5

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.

Lick answered 18/9, 2013 at 15:19 Comment(0)
D
5

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();
Deaden answered 28/5, 2015 at 6:27 Comment(0)
C
0

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();
Cloudscape answered 22/11, 2012 at 13:39 Comment(0)
Z
0

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..

Zenobiazeolite answered 14/3, 2013 at 20:22 Comment(0)
P
0

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.

    }
Purview answered 16/4, 2013 at 5:27 Comment(1)
This answer needs more explanation to be useful.Flotow
O
0

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

Oday answered 30/10, 2013 at 9:21 Comment(0)
I
0

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);

Intensity answered 24/6, 2014 at 14:26 Comment(0)
D
0

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.

Dacha answered 11/5, 2015 at 22:41 Comment(0)
T
0

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.

Teryn answered 7/10, 2015 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.