问题
I am trying to record voice in android emulator (android 4.1).
But I when I press the button to start recording then it immediately return from the function and executes next line that logs a message saying "Recorded".
Then, I press the stop button and in Logcat message appears that media recorder went away with unhandled events.
If I again press the button to start recording then I get an error message saying FATAL SIGNAL 11. And I dont know how to access the SD card to see if the file is created or not.
Below is the logCat code that I used from a tutorial:
}
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
try{
recorder.prepare();
}
catch(IOException e){
Log.e("Recorder","Recording failed");
}
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
The logCat is:
08-08 16:09:39.713: D/gralloc_goldfish(743): Emulator without GPU emulation detected.
08-08 16:09:42.674: D/Recorder(743): Recorded
08-08 16:09:48.764: W/MediaRecorder(743): mediarecorder went away with unhandled events
08-08 16:13:01.613: A/libc(743): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 743 (xample.recorder)
回答1:
I solved this problem by resting recorder before releasing it.
recorder.stop();//stop recording
recorder.reset();
recorder.release();
回答2:
Each time create a new instance of recorder and you wont get any error.
recorder.stop();
recorder.release();
recorder=null;
And let the following be the first line in your startRecording() method-
recorder=new MediaRecorder();
For more info-http://developer.android.com/guide/topics/media/audio-capture.html
来源:https://stackoverflow.com/questions/11863488/trying-to-record-voice-in-android-but-getting-error