Этот ниже код - это мое простое звуковое приложение для записи, и я пытаюсь записать звук с помощью микрофона. после записи звука я не могу остановить запись и я получаю сообщение об ошибке:Запись аудиозаписей в Android. Java.lang.IllegalStateException error
java.lang.IllegalStateException
для этой линии:
recorder.stop();
Мой код:
public class MainActivity extends Activity implements OnClickListener {
Button startRecordingButton, stopRecordingButton;
TextView statusText;
private MediaRecorder recorder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
recorder = new MediaRecorder();
statusText = (TextView) this.findViewById(R.id.StatusTextView);
startRecordingButton = (Button) this
.findViewById(R.id.StartRecordingButton);
stopRecordingButton = (Button) this
.findViewById(R.id.StopRecordingButton);
startRecordingButton.setOnClickListener(this);
stopRecordingButton.setOnClickListener(this);
stopRecordingButton.setEnabled(false);
}
public void onClick(View v) {
if (v == startRecordingButton) {
record();
} else if (v == stopRecordingButton) {
stopRecording();
}
}
public void record() {
startRecordingButton.setEnabled(false);
stopRecordingButton.setEnabled(true);
recordAudio();
}
private void recordAudio() {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath());
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
public void stopRecording() {
recorder.stop();
recorder.release();
}
}
Пожалуйста proivde в StackTrace. –
'setOutputFile (Environment.getExternalStorageDirectory(). GetAbsolutePath());' выглядит странно. Вы не указываете имя файла. – Michael