我使用AudioRecorder錄製短音頻剪輯,但我調用AudioRecord.start()時我得到IllegalStateException我一直在尋找小時,但無法找到這個原因...IllegalStateException當調用AudioRecord.start()
我已設置音頻錄製+寫入外部存儲權限。
這裏有一塊我的代碼:
// main activity...
// Audio inits
final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getTempPath());
...
// called the sound rec async
new SoundComponent(tvmic, pb, tb).execute(recorder);
// SoundComponent.java
// Getting IllegalStateException when calling recorder[0].start();
[..]
protected Long doInBackground(MediaRecorder... recorder) {
try {
recorder[0].prepare();
} catch (IOException e) {
Log.e("100", "prepare() failed");
}
while (tb.isChecked())
{
//publishProgress();
//recorder[0].prepare();
recorder[0].start(); // here it were it throws
try {
Thread.sleep(250);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// int amplitude = recorder[0].getMaxAmplitude();
recorder[0].stop();
}
// TODO Auto-generated method stub
return null;
}
[..]
public String getTempPath() // audio temp path
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path+="/temp/audiorectemp.3gp";
return path;
}
爲什麼你使用'AsyncTask'來做到這一點? – Squonk
這是更復雜的應用程序的一部分。我已經完成視頻採集和傳感器處理使用異步沒有問題。 –
無論是否成功準備,您都將開始錄製對象。如果應該輸入'IOException' catch塊(即「prepare()失敗」),你應該處理它不同的問題。 – kcoppock