2013-11-09 135 views
0

我工作的一個應用程序,需要在錄製視頻的Android - MediaRecorder啓動()拋出IllegalStateException異常

這裏是我的部分代碼

mRecorder = new MediaRecorder(); 
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
String mFileName = Environment.getExternalStorageDirectory() 
      .getAbsolutePath(); 
mFileName += "/youraudiofile.3gp"; 
mSurfaceHolder.getSurface(); 
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); 
mRecorder.setOutputFile(mFileName); 
mRecorder.setVideoSize(320, 240); 
mRecorder.setVideoFrameRate(15); 
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); 
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
mRecorder.setMaxDuration(30000); 
try { 
mRecorder.prepare(); 
mRecorder.start(); 
} catch (IllegalStateException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

上面的代碼拋出IllegalStateException異常。我擁有在AndroidManifest.xml中輸入的所有權限,我非常確定AndroidManifest.xml中沒有任何錯誤。

我已經閱讀了幾個解決方案,但他們都沒有工作。我怎麼解決這個問題?

日誌貓下面

 
11-09 20:47:39.787: E/AndroidRuntime(29000): FATAL EXCEPTION: main 
11-09 20:47:39.787: E/AndroidRuntime(29000): java.lang.IllegalStateException: Could not execute method of the activity 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.view.View$1.onClick(View.java:3624) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.view.View.performClick(View.java:4117) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.view.View$PerformClick.run(View.java:17052) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.os.Handler.handleCallback(Handler.java:615) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.os.Looper.loop(Looper.java:137) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.app.ActivityThread.main(ActivityThread.java:4812) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at java.lang.reflect.Method.invokeNative(Native Method) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at java.lang.reflect.Method.invoke(Method.java:511) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at dalvik.system.NativeStart.main(Native Method) 
11-09 20:47:39.787: E/AndroidRuntime(29000): Caused by: java.lang.reflect.InvocationTargetException 
11-09 20:47:39.787: E/AndroidRuntime(29000): at java.lang.reflect.Method.invokeNative(Native Method) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at java.lang.reflect.Method.invoke(Method.java:511) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.view.View$1.onClick(View.java:3619) 
11-09 20:47:39.787: E/AndroidRuntime(29000): ... 11 more 
11-09 20:47:39.787: E/AndroidRuntime(29000): Caused by: java.lang.RuntimeException: start failed. 
11-09 20:47:39.787: E/AndroidRuntime(29000): at android.media.MediaRecorder.start(Native Method) 
11-09 20:47:39.787: E/AndroidRuntime(29000): at com.example.camera2.CameraView.startRecording(CameraView.java:149) 
11-09 20:47:39.787: E/AndroidRuntime(29000): ... 14 more 

提供的修飾的代碼如下,即使setOutputFormat(),setVideoSource(),setAudioSource()出現的正上方,如果{},setProfile()將拋出RuntimeException 。 下面的代碼將拋出IllegalStateException同以前的日誌

mRecorder = new MediaRecorder(); 

    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    String mFileName = Environment.getExternalStorageDirectory() 
      .getAbsolutePath(); 
    mFileName += "/youraudiofile.3gp"; 
    mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); 

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
     mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 
    } else { 

     if (! isFrontCamera) { 
      CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
      mRecorder.setProfile(camcorderProfile); 

     } else { 
      CamcorderProfile camcorderProfile = CamcorderProfile.get(findFrontFacingCamera(), CamcorderProfile.QUALITY_LOW); 
      mRecorder.setProfile(camcorderProfile); 
     } 

     mRecorder.setVideoSize(720, 480); 
    } 
    mRecorder.setVideoFrameRate(15); 
    mRecorder.setOutputFile(mFileName); 
    mRecorder.setMaxDuration(30000); 

    try { 
     mRecorder.prepare(); 
     mRecorder.start(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

發表你的logcat .. –

+0

剛剛發佈 –

+0

ok ..我會運行它等待.. –

回答

2

嘗試刪除這條線,並檢查一次..

mRecorder.setVideoSize(320, 240); 

一些你的設置不工作befor 3.0所以像這樣設置他們..

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 

     mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
     mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
     mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 

    } else { 

     if (! isFrontCamera) { 
      CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
      mMediaRecorder.setProfile(camcorderProfile); 

     } else { 
      CamcorderProfile camcorderProfile = CamcorderProfile.get(mFrontCamId, CamcorderProfile.QUALITY_LOW); 
      mMediaRecorder.setProfile(camcorderProfile); 
     } 

     mMediaRecorder.setVideoSize(720, 480); 
    } 
+0

按照你的解決方案後,我得到的錯誤是相同的。不知道出了什麼問題,或者我仍然是新手。我會盡快更新我的代碼。 –

0

你有你的清單這些權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.RECORD_VIDEO" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO"/> 
+0

一切都按照我在我的陳述 –

+0

確定的一個陳述中添加。 1)看,你的硬件是否支持這些格式(+輸出格式),你定義了什麼。 2)嘗試刪除setVideoFrameRate – MrKalach

+0

嘗試,我確信我的硬件支持所有這些格式 –

相關問題