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

后端 未结 9 1190
夕颜
夕颜 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.

    0 讨论(0)
  • 2021-02-04 01:50

    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.

    0 讨论(0)
  • 2021-02-04 01:50

    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();
    
    0 讨论(0)
  • 2021-02-04 01:55

    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.
    
        }
    
    0 讨论(0)
  • 2021-02-04 01:56

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

    0 讨论(0)
  • 2021-02-04 01:59

    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.

    0 讨论(0)
提交回复
热议问题