0
我的應用程序啓動thread A
來保存一些數據。在這個線程我調用函數startRecording(audioFile.getAbsolutePath());
Android上錄音時出錯
,但我得到了以下錯誤:
start called in an invalid state: 16; at android.media.MediaRecorder.start(Native Method)
這個錯誤沒有發生每次,但有時我從我的應用程序這個錯誤報告。
下面是我的代碼。
public void startRecording(String outputPath) {
if (recorder == null) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioSamplingRate(RECORDER_SAMPLERATE_22050);
recorder.setOutputFile(outputPath);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
recorder.start();
SampleRecordThread thread = new SampleRecordThread(outputPath);
thread.start();
}
private class SampleRecordThread extends Thread {
private volatile boolean running = true;
public void exit() {
running = false;
}
@Override
public void run() {
while (running) {
try {
Thread.sleep(10 * 1000);//to record 10 seconds sound
} catch (InterruptedException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
// upload the data to cloud
if (isWifiActive && isInstallationSaved) {
.....
record.saveInBackground();
}
break;
}
}
prbably將是有益的:[鏈接](http://stackoverflow.com/questions/ 19709677/mediarecorder-invalid-state-16) –
固定的語法和風格 –