2012-09-26 87 views
2

我剛剛開始使用開發人員教程。我修改了下面的錄製代碼,但是我收到一個錯誤,但我不確定。我沒有一個深入的Java級別,而且我只有3周左右進入對象和類。這個應用程序是爲一個項目,我們沒有規範。在Android應用程序中錄製媒體時發生崩潰

private static final String LOG_TAG = "Recording"; 

//start recording stuff 

private RecordButton mRecordButton = null; 
private MediaRecorder mRecorder = null; 

private void onRecord(boolean start) 
{ 
    if (start) 
     startRecording(); 
    else 
     stopRecording(); 

} 

private void startRecording() 
{ 
    mRecorder = new MediaRecorder(); 
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    mRecorder.setOutputFile("/sdCard0/panic_record/" + System.currentTimeMillis()+ ".3gp"); 
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    try { 
     mRecorder.prepare(); 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "prepare() failed"); 
    } 

    mRecorder.start(); 
} 

private void stopRecording() 
{ 
    mRecorder.stop(); 
    mRecorder.release(); 
    mRecorder = null; 
} 

class RecordButton extends Button { 
    boolean mStartRecording = true; 

    OnClickListener clicker = new OnClickListener() { 
     public void onClick(View v) { 
      onRecord(mStartRecording); 
      if (mStartRecording) { 
       setText("Stop recording"); 
      } else { 
       setText("Start recording"); 
      } 
      mStartRecording = !mStartRecording; 
     } 
    }; 

    public RecordButton(Context ctx) { 
     super(ctx); 
     setText("Start recording"); 
     setOnClickListener(clicker); 
    } 


} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_record); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    LinearLayout ll = new LinearLayout(this); 
    mRecordButton = new RecordButton(this); 
    ll.addView(mRecordButton, 
     new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      0)); 

    setContentView(ll); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_record, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

logcat的輸出

 
09-26 20:05:37.620: E/Recording(1152): prepare() failed 
09-26 20:05:37.620: E/MediaRecorder(1152): start called in an invalid state: 4 
09-26 20:05:37.620: D/AndroidRuntime(1152): Shutting down VM 
09-26 20:05:37.620: W/dalvikvm(1152): threadid=1: thread exiting with uncaught exception (group=0x41c18300) 
09-26 20:05:37.630: E/AndroidRuntime(1152): FATAL EXCEPTION: main 
09-26 20:05:37.630: E/AndroidRuntime(1152): java.lang.IllegalStateException 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.media.MediaRecorder.start(Native Method) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at cs4084.panic.button.Record.startRecording(Record.java:55) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at cs4084.panic.button.Record.onRecord(Record.java:34) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at cs4084.panic.button.Record.access$0(Record.java:31) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at cs4084.panic.button.Record$RecordButton$1.onClick(Record.java:71) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.view.View.performClick(View.java:4211) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.view.View$PerformClick.run(View.java:17267) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.os.Handler.handleCallback(Handler.java:615) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.os.Handler.dispatchMessage(Handler.java:92) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.os.Looper.loop(Looper.java:137) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at android.app.ActivityThread.main(ActivityThread.java:4898) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at java.lang.reflect.Method.invoke(Method.java:511) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
09-26 20:05:37.630: E/AndroidRuntime(1152):  at dalvik.system.NativeStart.main(Native Method) 

回答

5

在你的情況下,準備失敗。而且,由於您正在捕捉IO異常,我想這可能是由於無法使用文件夾/ sdCard0/panic_record /進行寫訪問。

通常sdcard文件夾將是/ sdcard而不是/ sdCard0(注意大寫的C)。更好的辦法是

String path = Environment.getExternalStorageDirectory().getAbsolutePath() 
+ "/panic_record/" + System.currentTimeMillis()+ ".3gp"; 

mRecorder.setOutputFile(path); 

不要忘記添加WRITE_EXTERNAL_STORAGE許可

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

非常感謝。我有權限設置,但該行解決了它。欣賞它。這個網站一直是幫助我的知識堡壘! –

+0

[接受](http://meta.stackexchange.com/a/5235/176658)答案如果有幫助:) – nandeesh

2

這是你的異常的相關位:

09-26 20:05:37.630: E/AndroidRuntime(1152): FATAL EXCEPTION: main 
09-26 20:05:37.630: E/AndroidRuntime(1152): java.lang.IllegalStateException 
09-26 20:05:37.630: E/AndroidRuntime(1152): at android.media.MediaRecorder.start(Native Method) 
09-26 20:05:37.630: E/AndroidRuntime(1152): at cs4084.panic.button.Record.startRecording(Record.java:55) 
... 

閱讀 「拋出」 這裏的start方法的文檔的部分:http://developer.android.com/reference/android/media/MediaRecorder.html#start()