2013-04-12 118 views
1

Posical複製MediaRecorder start failed: -16MediaRecorder啓動失敗:-16時變化CamcorderProfile.QUALITY_LOW到CamcorderProfile.QUALITY_HIGH

MediaRecorder啓動失敗:-16時變化CamcorderProfile.QUALITY_LOWCamcorderProfile.QUALITY_HIGH。我已經嘗試了很多解決方案,但得到相同的錯誤。

以下是我的代碼:

private boolean prepareVideoRecorder() { 

     if(mMediaRecorder == null){ 
     mMediaRecorder = new MediaRecorder(); 
     }else{ 
      Log.d(Constants.TAG,"MediaRecoder is Not Null"); 
     } 
     // Step 1: Unlock and set camera to MediaRecorder 
     mCamera.stopPreview(); 
     mCamera.unlock(); 
     mMediaRecorder.setCamera(mCamera); 

     // Step 2: Set sources 
     mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
     mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
//  Size optimalSize = new Size(320,240); 
//  int width=320, height=240; 
//  Parameters params = mCamera.getParameters(); 
//  List<Size> sizes = params.getSupportedPreviewSizes(); 
//  optimalSize = getOptimalPreviewSize(sizes, width, height); 
//  params.setPreviewSize(optimalSize.width, optimalSize.height); 
//  mMediaRecorder.setVideoSize(optimalSize.width, optimalSize.height); 
      CamcorderProfile profile = CamcorderProfile.get(mCamSelect, CamcorderProfile.QUALITY_HIGH); 
      if(profile == null){ 
       Log.d(Constants.TAG, "the camcorder profile instance is null"); 
      } 
     // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
     mMediaRecorder.setProfile(profile); 

     // Step 4: Set output file 
     mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); 

     // Step 5: Set the preview output 
     mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 

     // Step 6: Prepare configured MediaRecorder 
     try { 

      mMediaRecorder.prepare(); 
     } catch (IllegalStateException e) { 
      Log.d("DEBUG", "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     } catch (IOException e) { 
      Log.d("DEBUG", "IOException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     } 
     return true; 
    } 

的logcat:

04-12 11:09:14.396: V/MediaRecorderJNI(19761): start 
04-12 11:09:14.396: V/MediaRecorderJNI(19761): getMediaRecorder E 
04-12 11:09:14.466: E/MediaRecorder(19761): start failed: -16 
04-12 11:09:14.466: V/MediaRecorderJNI(19761): process_media_recorder_call 
04-12 11:09:14.466: D/AndroidRuntime(19761): Shutting down VM 
04-12 11:09:14.466: W/dalvikvm(19761): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
04-12 11:09:14.466: E/AndroidRuntime(19761): FATAL EXCEPTION: main 
04-12 11:09:14.466: E/AndroidRuntime(19761): java.lang.RuntimeException: start failed. 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.media.MediaRecorder.start(Native Method) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at uk.org.humanfocus.hfi.RecordVideoActivity.captureButtonHandler(RecordVideoActivity.java:218) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at uk.org.humanfocus.hfi.RecordVideoActivity.onClick(RecordVideoActivity.java:100) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.view.View.performClick(View.java:2533) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.view.View$PerformClick.run(View.java:9320) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.os.Handler.handleCallback(Handler.java:587) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.os.Looper.loop(Looper.java:150) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at android.app.ActivityThread.main(ActivityThread.java:4385) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at java.lang.reflect.Method.invokeNative(Native Method) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at java.lang.reflect.Method.invoke(Method.java:507) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
04-12 11:09:14.466: E/AndroidRuntime(19761): at dalvik.system.NativeStart.main(Native Method) 
+0

有人能幫助我????????????? –

+0

你解決了嗎?瓊脂g the請回復。 –

+0

是固定的。代碼發佈在答案中。如果它能解決您的問題,請投票。 –

回答

2

我解決了這個問題。

以下是我的工作代碼。

private boolean prepareVideoRecorder() { 

     if(mMediaRecorder == null){ 
      mMediaRecorder = new MediaRecorder(); 
     }else{ 
      Log.d(Constants.TAG,"MediaRecoder is Not Null"); 
     } 

     // Step 1: Unlock and set camera to MediaRecorder 
     mCamera.stopPreview(); 
     mCamera.unlock(); 
     mMediaRecorder.setCamera(mCamera); 


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

     // Step 3: Set output format and encoding (for versions prior to API Level 8) 
     CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
     camcorderProfile.videoFrameWidth = 640; 
     camcorderProfile.videoFrameHeight = 480; 
//  camcorderProfile.videoFrameRate = 15; 
     camcorderProfile.videoCodec = MediaRecorder.VideoEncoder.H264; 
//  camcorderProfile.audioCodec = MediaRecorder.AudioEncoder.AAC; 
     camcorderProfile.fileFormat = MediaRecorder.OutputFormat.MPEG_4; 

     mMediaRecorder.setProfile(camcorderProfile); 

     // Step 4: Set output file 
     mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); 

     // Step 5: Set the preview output 
     mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 

     // Step 6: Prepare configured MediaRecorder 
     try { 

      mMediaRecorder.prepare(); 
     } catch (IllegalStateException e) { 
      Log.d("DEBUG", "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     } catch (IOException e) { 
      Log.d("DEBUG", "IOException preparing MediaRecorder: " + e.getMessage()); 
      releaseMediaRecorder(); 
      return false; 
     } 
     return true; 
    } 
+0

是的,這工作。但是你已經對'camcorderProfile.videoFrameWidth'和'camcorderProfile.videoFrameHeight'進行了硬編碼。有沒有辦法將它們設置爲默認值? –

+0

@AdilMalik IT是我的要求。你可以嘗試通過評論這些行。 –

+0

我已經試過了。但它沒有奏效。我不得不使用我在博客上找到的另一個代碼。如果有人對解析沒有嚴格的要求,那麼你的代碼工作正常。 –