7

我搜索了很多主題,但沒有直接的答案。MediaRecorder啓動時崩潰

我有這樣的代碼:

 recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 

    recorder.setOutputFile(mFileName); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    if(!mStartRecording) 
    { 
     btn.setText("Stop Recording"); 
     try { 
      recorder.prepare(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     recorder.start(); 
     mStartRecording = true; 
    } 
    else 
    { 
     btn.setText("Start Recording"); 
     mStartRecording = false; 
     recorder.stop(); 
     recorder.reset(); 
     recorder.release(); 

     recorder = null; 
    } 

而且我補充說:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.STORAGE" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 

(見的地方,添加存儲解決它,不適合我)

我在API級別7(Android 2.1)上開發

堆棧跟蹤顯示「啓動顯示」 堆棧軌跡:

04-26 19:27:41.955: D/dalvikvm(890): GC freed 809 objects/58272 bytes in 433ms 
04-26 19:27:44.772: D/dalvikvm(890): GC freed 95 objects/3936 bytes in 371ms 
04-26 19:28:54.973: E/MediaRecorder(890): start failed: -1 
04-26 19:28:54.993: D/AndroidRuntime(890): Shutting down VM 
04-26 19:28:54.993: W/dalvikvm(890): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
04-26 19:28:54.993: E/AndroidRuntime(890): Uncaught handler: thread main exiting due to uncaught exception 
04-26 19:28:55.105: E/AndroidRuntime(890): java.lang.IllegalStateException: Could not execute method of the activity 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.View$1.onClick(View.java:2031) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.View.performClick(View.java:2364) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.View.onTouchEvent(View.java:4179) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.widget.TextView.onTouchEvent(TextView.java:6541) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.View.dispatchTouchEvent(View.java:3709) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
04-26 19:28:55.105: E/AndroidRuntime(890): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 
04-26 19:28:55.105: E/AndroidRuntime(890): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 
04-26 19:28:55.105: E/AndroidRuntime(890): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.os.Looper.loop(Looper.java:123) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-26 19:28:55.105: E/AndroidRuntime(890): at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 19:28:55.105: E/AndroidRuntime(890): at java.lang.reflect.Method.invoke(Method.java:521) 
04-26 19:28:55.105: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-26 19:28:55.105: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-26 19:28:55.105: E/AndroidRuntime(890): at dalvik.system.NativeStart.main(Native Method) 
04-26 19:28:55.105: E/AndroidRuntime(890): Caused by: java.lang.reflect.InvocationTargetException 
04-26 19:28:55.105: E/AndroidRuntime(890): at shibby.whisper.WhisperGameActivity.recordAudio(WhisperGameActivity.java:94) 
04-26 19:28:55.105: E/AndroidRuntime(890): at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 19:28:55.105: E/AndroidRuntime(890): at java.lang.reflect.Method.invoke(Method.java:521) 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.view.View$1.onClick(View.java:2026) 
04-26 19:28:55.105: E/AndroidRuntime(890): ... 21 more 
04-26 19:28:55.105: E/AndroidRuntime(890): Caused by: java.lang.RuntimeException: start failed. 
04-26 19:28:55.105: E/AndroidRuntime(890): at android.media.MediaRecorder.start(Native Method) 
04-26 19:28:55.105: E/AndroidRuntime(890): ... 25 more 
04-26 19:28:55.223: I/dalvikvm(890): threadid=7: reacting to signal 3 
04-26 19:28:55.335: I/dalvikvm(890): Wrote stack trace to '/data/anr/traces.txt' 
04-26 19:28:57.123: I/Process(890): Sending signal. PID: 890 SIG: 9 

請幫忙。

+0

提供所有的堆棧跟蹤請 – Snicolas 2012-04-26 19:51:34

+0

@Snicolas提供。 – 2012-04-26 19:53:59

+0

你能在/data/anr/traces.txt找到什麼 – Snicolas 2012-04-26 19:56:22

回答

3

好吧,我知道了。我想你已經將mStartRecording初始化爲true。

因此,您的if正在進入else區塊。其中,您停止了一個全新的MediaRecorder實例,並且狀態圖不允許這樣做。

讓您的媒體記錄器成爲您班級的一個領域。並正確地初始化你的mStartRecording布爾變量爲false。僅當您的字段爲空時,請重新設置媒體記錄器。

if(recorder == null) { 
    recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 

    recorder.setOutputFile(mFileName); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
}//if 
if(!mStartRecording) { 
    btn.setText("Stop Recording"); 
    try { 
     recorder.prepare(); 
     recorder.start(); 
     mStartRecording = true; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }//catch 
} else { 
    btn.setText("Start Recording"); 
    mStartRecording = false; 
    recorder.stop(); 
    recorder.reset(); 
    recorder.release(); 
    recorder = null; 
}//else 
+0

對不起,我沒有提到,我認爲這是一個給定的。 mStartRecording是錯誤的。 – 2012-04-26 20:09:00

+0

試試吧。它仍然是一個更好的方式來編碼 – Snicolas 2012-04-26 20:14:30

+1

我試過了,還是一樣的錯誤。 – 2012-04-26 20:17:33

2

嘗試將start功能放在與prepare功能相同的功能塊中。也許有一個異常阻塞從執行準備並直接啓動,從而導致IllegalStateException

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(output_formats[currentFormat]); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(getFilename()); 


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

開始時就像那樣,仍然沒有工作。 – 2012-04-26 20:01:47

+0

完全相同的跟蹤... – 2012-04-26 20:05:48

+0

是setaudioencoder之後的setoutputfile嗎?嘗試改變它! – 2012-04-26 20:13:11

0

嘗試啓動錄音機,只有當它準備:

try { 
     recorder.prepare(); 
     recorder.start(); 
     mStartRecording = true; 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "Error when preparing or starting recorder", e); 
    } 
+0

我做到了。仍然沒有工作... – 2012-04-26 20:03:49

1

我用下面的代碼,完全適用於我..

protected void startRecording() { 
    // TODO Auto-generated method stub 
    i++; 
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    mFileName += "/audiorecordtest"+i+".3gp"; 
    recorder = new MediaRecorder(); 

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setOutputFile(mFileName); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     try { 
     recorder.prepare(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show(); 


    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show(); 

    } 

     recorder.start(); 
} 

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

這些方法必須安排它的運行順序。這裏:

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setOutputFile(mFileName); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
0

我有同樣的問題。這是因爲我在設置錄製音頻的文件名時錯過了斜線。

變化

this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
this.fileName += "yourfilename.3gp"; 

this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
this.fileName += "/yourfilename.3gp"; 
0

我,因爲我的SurfaceView的同樣的問題已不可見。所以使它可見

mSurfaceView.setVisibility(View.VISIBLE);