title: MediaRecorder: Stop failed -1007
body: <p>I have seen questions like that but I have not found solution, so that is why I am asking this.</p>
  
  <p>When I run my app on Alcatel phone and start video recording, everything seems fine, until I press stop button, then I get <code>RuntimeException</code>. I read that my video or audio is not in good state, but I can not figure out why.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>private boolean prepareMediaRecorder(String name) {
  
      if (isCameraOpened()) {
          stop();
          start();
      }
  
      mediaRecorder = new MediaRecorder();
      //int orientation = mCameraInfo.orientation;
      //int orientation = calcCameraRotation(mDisplayOrientation);
      int degrees = 0;
      switch (currentOrientation) {
          case Surface.ROTATION_0:
              degrees = 0;
              break;
          case Surface.ROTATION_90:
              degrees = 90;
              break;
          case Surface.ROTATION_180:
              degrees = 180;
              break;
          case Surface.ROTATION_270:
              degrees = 270;
              break;
      }
  
      int result;
      /*if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          result = (360 - (mCameraInfo.orientation + degrees) % 360) % 360;
      } else {  // back-facing
          result = (mCameraInfo.orientation - degrees + 360) % 360;
      }*/
      if(mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
          result = (mCameraInfo.orientation - degrees + 360) % 360;
         // mediaRecorder.setOrientationHint(result);
          mediaRecorder.setOrientationHint(result);
      } else{
          result = (mCameraInfo.orientation + degrees) % 360;
          mediaRecorder.setOrientationHint(result);
      }
      Size pictureSize = null;
  
      SortedSet&lt;Size&gt; sizes = mPreviewSizes.sizes(mAspectRatio);
      if (sizes == null) { // Not supported
          mAspectRatio = chooseAspectRatio();
          sizes = mPreviewSizes.sizes(mAspectRatio);
      }
  
      mPictureSizes.clear();
  
      for (Camera.Size size : mCameraParameters.getSupportedVideoSizes()) {
          mPictureSizes.add(new Size(size.width, size.height));
      }
  
      Size size = chooseOptimalSize(sizes);
  
      final Camera.Size currentSize = mCameraParameters.getPictureSize();
      if (currentSize.width != size.getWidth() || currentSize.height != size.getHeight()) {
          // Largest picture size in this ratio
          pictureSize = mPictureSizes.sizes(mAspectRatio).last();
          if (mShowingPreview) {
              mCamera.stopPreview();
          }
  
          if(mCameraParameters.getSupportedPreviewSizes() != null) {
              mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
          }
  
          setAutoFocusInternal(mAutoFocus);
          if(mFlash == 1){
              mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
          }
          mCamera.setParameters(mCameraParameters);
          if (mShowingPreview) {
              mCamera.startPreview();
          }
      } 
  
      mCamera.unlock();
  
      mediaRecorder.setCamera(mCamera);
  
      //Set source
      mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  
      CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
      mediaRecorder.setProfile(cpHigh);
      if(pictureSize != null) {
          mediaRecorder.setVideoSize(pictureSize.getWidth(), pictureSize.getHeight());
      }
  
      mediaRecorder.setOutputFile(name);
  
  
      try {
          mediaRecorder.prepare();
          ///mediaRecorder.start();
  
      } catch (IllegalStateException e) {
          releaseMediaRecorder();
          return false;
      } catch (IOException e) {
          releaseMediaRecorder();
          return false;
      }
      return true;
  
  }
  </code></pre>
  
  <p>Then I start MediaRecorder in AsyncTask and this is my stop method.</p>
  
  <pre><code>@Override
  void stopVideo() {
      if(mFlash == 1){
          mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
      }
      if(isVideoRecording == true) {
          try {
              mediaRecorder.stop();
              isVideoRecording = false;
              releaseMediaRecorder();
              getBackgroundHandler().post(new Runnable() {
                  @Override
                  public void run() {
                      File file = getFile(name);
                      if (file != null) {
                          ContentValues values = new ContentValues();
                          values.put(MediaStore.Video.Media.TITLE,  file.getName());
                          values.put(MediaStore.Video.Media.DESCRIPTION, "Taken by All Ears Selfie app");
                          values.put(MediaStore.Video.Media.DATE_TAKEN,   System.currentTimeMillis());
                          values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
                          values.put(MediaStore.Video.Media.SIZE,  file.length());
                          values.put("_data", file.getAbsolutePath());
  
                          ContentResolver cr = context.getContentResolver();
                           cr.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
  
                      }
                  }
              });
          } catch (RuntimeException e){
              Log.d("TAG", e.getMessage());
              File file = getFile(name);
              file.delete();
          }
  
      }
  
      if(isCameraOpened()){
          stop();
          start();
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>11-15 19:37:09.063 15282-15282/com.allearsapp.allearsselfie E/MediaRecorder: stop failed: -1007
    11-15 19:37:09.064 15282-15282/com.allearsapp.allearsselfie D/AndroidRuntime: Shutting down VM
    11-15 19:37:09.089 15282-15282/com.allearsapp.allearsselfie E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.allearsapp.allearsselfie, PID: 15282
                                                                                  java.lang.RuntimeException: stop failed.
                                                                                      at android.media.MediaRecorder.stop(Native Method)
                                                                                      at com.google.android.cameraview.Camera1.stopVideo(Camera1.java:314)
                                                                                      at com.google.android.cameraview.CameraView.stopVideo(CameraView.java:64)
                                                                                      at com.allearsapp.allearsselfie.camera.CameraActivity.stopVideoRecording(CameraActivity.java:410)
                                                                                      at com.allearsapp.allearsselfie.camera.CameraActivity.access$300(CameraActivity.java:54)
                                                                                      at com.allearsapp.allearsselfie.camera.CameraActivity$2.onClick(CameraActivity.java:288)
                                                                                      at android.view.View.performClick(View.java:4757)
                                                                                      at android.view.View$PerformClick.run(View.java:19757)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5258)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> I added <code>OnErrorListener</code> for Media Recorder and I get this error: 268435556.</p>