2012-05-08 30 views
6

Tôi đang cố gắng tạo một máy quay video trên Android, và tôi đã chuẩn bị mã của tôi được cho là đang hoạt động - nhưng tôi liên tục nhận được thông báo lỗi start failed: -19.Android MediaRecorder - "bắt đầu thất bại: -19"

Dưới đây là mã của tôi:

public boolean startRecording() { 
    try { 
     camera.unlock(); 
     mediaRecorder = new MediaRecorder(); 
     mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() { 

       @Override 
       public void onError(MediaRecorder mr, int what, int extra) { 
       Log.i(TAG, "Error"); 
      } 
     }); 

     mediaRecorder.setCamera(camera); 
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
     Log.i(TAG, "a"); 

     mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
     mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); 
     Log.i(TAG, "b"); 

     mediaRecorder.setMaxDuration(maxDurationInMs); // set to 20000 

     String uniqueOutFile = OUTPUT_FILE + System.currentTimeMillis() + ".3gp"; 
     File outFile = new File(uniqueOutFile); 
     if (outFile.exists()) { 
      outFile.delete(); 
     } 
     mediaRecorder.setOutputFile(uniqueOutFile); 
     mediaRecorder.setVideoFrameRate(videoFramesPerSecond); // set to 20 
     mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight()); 
     Log.i(TAG, "c"); 

     mediaRecorder.setPreviewDisplay(holder.getSurface()); 
     mediaRecorder.setMaxFileSize(maxFileSizeInBytes); // set to 50000 
     mediaRecorder.prepare(); 
     Log.i(TAG, "d"); 

     mediaRecorder.start(); 
     Log.i(TAG, "e"); 

     return true; 
     } catch (IllegalStateException e) { 
      Log.i(TAG, "f"); 
      Log.e(TAG, e.getMessage()); 
      e.printStackTrace(); 
      camera.lock(); 
      return false; 
     } catch (IOException e) { 
      Log.i(TAG, "g"); 
      Log.e(TAG, e.getMessage()); 
      e.printStackTrace(); 
      camera.lock(); 
      return false; 
     } catch (RuntimeException e) { 
      Log.i(TAG, "h"); 
      Log.e(TAG, e.getMessage()); 
      camera.lock(); 
      return false; 
     } 
    } 

Tất cả các bản ghi gỡ lỗi (từ "a" thông qua "d") được in trong nhật ký, vì vậy nó có vẻ như tất cả các bước tối đa mediaRecorder.prepare() được thực hiện đúng cách. Sau đó, nó bắt được RuntimeException với thông báo start failed: -19. Có một số question tương tự, nhưng điều đó không giải quyết được vấn đề của tôi.

Có lý do nào khác để nhận được lỗi như vậy không?

Trả lời

14

Chỉ cần phát hiện ra lỗi, trong dòng này:

mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight()); 

sau khi cho ý kiến ​​ra dòng này, mã chạy một cách hoàn hảo!

+0

không làm việc trên samsung Duos lớn – Neji

+4

Bình luận đường ra này sẽ ném lực lượng đóng trên Samsung Các thiết bị như GN2, GS. Đây không phải là một giải pháp. Mặc dù tôi chưa bắt đầu sử dụng Android từ lâu, nhưng bây giờ tôi đã bị bệnh Android!Họ chỉ cho phép các nhà sản xuất tự do làm cho các điện thoại không có tiêu chuẩn. – Dante

+2

Làm cách nào bạn có thể chỉ định kích thước video nếu bạn xóa dòng này? – TOP

2

tôi giải quyết vấn đề của tôi một lần tôi thêm này cho quay video

/** 
* Start video recording by cleaning the old camera preview 
*/ 
private void startVideoRecorder() { 
    // THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF 
    // "MediaRecorder start failed: -19" 
    // THIS WONT BE NEEDED INCASE OF PHONE AND TABLET 
    // This causes crash in glass kitkat version so remove it 
    // try { 
    // mCamera.setPreviewDisplay(null); 
    // } catch (java.io.IOException ioe) { 
    // Log.d(TAG, 
    // "IOException nullifying preview display: " 
    // + ioe.getMessage()); 
    // } 
    // mCamera.stopPreview(); 
    // mCamera.unlock(); 
    recorder = new MediaRecorder(); 
    // Let's initRecorder so we can record again 
    initRecorder(); 
} 

/** 
* Initialize video recorder to record video 
*/ 
private void initRecorder() { 
    try { 
     File dir = new File(folderPath); 
     if (!dir.exists()) { 
      dir.mkdirs(); 
     } 
     mCamera.stopPreview(); 
     mCamera.unlock(); 
     videofile = new File(dir, fileName + ".mp4"); 
     recorder.setCamera(mCamera); 

     // Step 2: Set sources 
     recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
     recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

     // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
     recorder.setProfile(CamcorderProfile 
       .get(CamcorderProfile.QUALITY_HIGH)); 

     // Step 4: Set output file 
     recorder.setOutputFile(videofile.getAbsolutePath()); 
     // Step 5: Set the preview output 
     recorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 
     // Step 6: Prepare configured MediaRecorder 
     recorder.setMaxDuration(video_duration * 1000); 
     recorder.setOnInfoListener(new OnInfoListener() { 

      @Override 
      public void onInfo(MediaRecorder mr, int what, int extra) { 
       if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { 

        mCamera.stopPreview(); 
        releaseMediaRecorder(); 

        /* 
        * initiate media scan and put the new things into the 
        * path array to make the scanner aware of the location 
        * and the files you want to see 
        */MediaScannerConnection.scanFile(
          CuxtomCamActivity.this, 
          new String[] { videofile.getPath() }, null, 
          null); 

        Intent intent = new Intent(); 
        intent.putExtra(CuxtomIntent.FILE_PATH, 
          videofile.getPath()); 
        intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO); 
        setResult(RESULT_OK, intent); 
        finish(); 
       } 

      } 
     }); 
     recorder.prepare(); 
     recorder.start(); 
    } catch (Exception e) { 
     Log.e("Error Stating CuXtom Camera", e.getMessage()); 
    } 
} 
private void releaseMediaRecorder() { 
    if (recorder != null) { 
     recorder.reset(); // clear recorder configuration 
     recorder.release(); // release the recorder object 
     recorder = null; 
    } 
} 

Đối với hướng dẫn chi tiết tham khảo này Open Source Cuxtom Cam

0

vấn đề là trong mã setVideoSize() của bạn.

và tại sao mã lỗi này ...

Từ nghiên cứu tôi đã làm, mã lỗi -19 đến khi có một vấn đề với kích thước của video như được thiết lập bởi MediaRecorder#setVideoSize()

chạy này mã, và xem whitch màn hình mà máy ảnh của bạn trong điện thoại của bạn có thể hỗ trợ:

final List<Camera.Size> mSupportedVideoSizes = getSupportedVideoSizes(mCamera); 
     for (Camera.Size str : mSupportedVideoSizes) 
      Log.e(TAG, "mSupportedVideoSizes "+str.width + ":" + str.height + " ... " 
        + ((float) str.width/str.height)); 

và phương pháp là:

public List<Size> getSupportedVideoSizes(Camera camera) { 
     if (camera.getParameters().getSupportedVideoSizes() != null) { 
      return camera.getParameters().getSupportedVideoSizes(); 
     } else { 
      // Video sizes may be null, which indicates that all the supported 
      // preview sizes are supported for video recording. 
      return camera.getParameters().getSupportedPreviewSizes(); 
     } 
    } 
0

Tôi gặp sự cố đó với một số điện thoại cụ thể, tôi đã phát hiện ra rằng tôi không thể đặt kích thước hồ sơ bộ mã hóa trong một số thiết bị. Nhưng khi làm việc cho androids có vấn đề nó ngừng làm việc trên các thiết bị làm việc trước đó.

Vì vậy, cuối cùng logic thực hiện của tôi là một cái gì đó như:

  • Set chiều rộng/chiều cao
  • Hãy thử để bắt đầu ghi merdia
  • Trong trường hợp ngoại lệ, hãy thử một lần nữa mà không cần thiết lập chiều rộng/chiều cao

Loại logic thùng rác, nhưng đã hiệu quả.

tôi đã thiết lập một dự án github với thực hiện điều đó, hãy thử nó ra: https://github.com/rafaelsilverio/MediaRecorder

Các vấn đề liên quan