2012-05-08 178 views
6

我試圖在Android上創建一個錄像機,並且我已經準備好了應該工作的代碼 - 但我經常收到一條錯誤消息start failed: -19Android MediaRecorder - 「啓動失敗:-19」

這裏是我的代碼:

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

所有的調試日誌(從「a」到「d」)打印在日誌中,所以它似乎所有高達mediaRecorder.prepare()步驟都做好。然後它通過訊息start failed: -19捕獲了RuntimeException。有類似question,但這並不能解決我的問題。

是否有任何其他原因得到這樣的錯誤?

回答

14

剛剛發現的bug,在這條線:

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

註釋掉這一行後,代碼運行完美!

+0

不工作的三星盛大二重奏 – Neji

+4

談到這一行出將引發強制關閉三星設備,如GN2,GS。這不是一個解決方案。雖然我還沒有開始android,但現在我有點不舒服了!他們只是讓製造商自由地製造沒有標準的手機。 – Dante

+2

如果您刪除此行,您如何指定視頻大小? – TOP

2

我解決我的問題,一旦我添加了這個視頻錄製

/** 
* 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; 
    } 
} 

有關詳細指南請參閱本Open Source Cuxtom Cam

0

問題出在你的setVideoSize()代碼。

,爲什麼這個代碼錯誤...

從研究,我都做了,錯誤代碼-19談到關於由MediaRecorder#setVideoSize()

來看,這種設置時,有與視頻大小的問題代碼,看看whitch屏幕,您在您的設備攝像頭可以支持:

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

和方法是:

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

我有一些特定的手機的問題,我已經發現,我不能在其中一些設置camcoder配置文件的大小。但是,當它爲有問題的機器人工作,它停止工作在以前的工作設備。

所以最終我實現的邏輯是這樣的:

  • 設置寬度/高度
  • 嘗試啓動merdia記錄
  • 在例外的情況下,不設置寬度/高度
  • 再試一次

一種垃圾邏輯,但工作。

我設置與實施github上項目,嘗試一下:https://github.com/rafaelsilverio/MediaRecorder

相關問題