3

I use this code to prepare my MediaRecorder for recording video. After this I call the start() method, which doesn't crash, however when I call the stop() method a crash occurs and the RuntimeException stop failed is raised. I also notice that the video file which is saved in the device is broken and is only 32B. I'm assuming I have an error somewhere when setting up the device in the below method. Notice that I am trying to record from the surfaceView live preview which is displayed on screen ( like snapchat) not from the native camera app.

 private void initRecorder(Surface surface) throws IOException {
    // It is very important to unlock the camera before doing setCamera
    // or it will results in a black preview
    if(mCamera == null) {
        mCamera = Camera.open();
        mCamera.unlock();
    }

    if(mMediaRecorder == null)  mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setPreviewDisplay(surface);
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            Toast.makeText(getApplicationContext(),
                    Integer.toString(what) + "_____" + Integer.toString(extra), Toast.LENGTH_LONG)
                    .show();            }
    });

    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    //       mMediaRecorder.setOutputFormat(8);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(640, 480);
    mMediaRecorder.setOutputFile(getVideoFile());

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // This is thrown if the previous calls are not called with the
        // proper order
        e.printStackTrace();
    }

    mInitSuccesful = true;
}
Alk
  • 5,215
  • 8
  • 47
  • 116
  • If you are capturing from surface, then why do you need to unlock camera? Also if you are capturing from surface you need to check the supportedVideoSizes and use one of those sizes for recording – Bhargav Feb 25 '16 at 04:35
  • The only exceptions I get is the stop failed. My surface view displays a live preview of the camera. The code I am using here is code which is supposedly working which I found on another answer on here, I'm not myself too in depth with recording video on Android. I already coded the part which takes photos, which works fine and also makes use of the surface view. Now I am trying to add the same functionality for videos. – Alk Feb 25 '16 at 04:38
  • @Bhargav can you explain further how I can do that? – Alk Feb 25 '16 at 04:41
  • @mankee there are 2 things supportedPreviewSizes and supportedVideoSizes, if supportedVideoSize return null then you can use one of the supportedPreviewSIzes but if supportedVideoSize doesn't return null then you HAVE to use one the supportedVideoSizes for recording, i.e pass the size value to `mediaRecorder.setVideoSize` – Bhargav Feb 25 '16 at 05:16
  • @Bhargav ok so I get the parameters, I do if p.getsupportedvideosizes == null and then you lost me...how do I decide which size to set, I'm assuming they both return a list of more than one size – Alk Feb 25 '16 at 05:22
  • yes they return a list, you need select the optimal size, use your own logic to decide that, whichever size suits your needs – Bhargav Feb 25 '16 at 05:37
  • but how do i know what is returned? doesn't this depend on the device? – Alk Feb 25 '16 at 05:39
  • @Alk I have the same problem, did you find a solution? – user25 Jul 03 '18 at 08:07
  • for my Xiaomi 4 Prime/Pro rebooting helped.. – user25 Jul 03 '18 at 09:58
  • I found out that it works ok only for some devices, it sucks.. – user25 Jul 13 '18 at 17:33
  • I created a separate question for my problem https://stackoverflow.com/questions/51332386/mediarecorder-and-videosource-surface-stop-failed-1007-a-serious-android-bug – user25 Jul 13 '18 at 20:40

0 Answers0