You need to call mediaRecorder.prepare()
before calling start()
Android Audio Recording Error
-
24-03-2022 - |
문제
I am trying to implement a audio recording for learning, but I ran into exception causing app crashed without explicit clue how this could be solved. here is my code
private static final File SDCARD_ROOT = Environment.getExternalStorageDirectory();
private static final String APP_DIR = "test_audio";
private static final String AUDIO_SAMPLE_NAME = "myShake";
...............
...............
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appDir = new File(SDCARD_ROOT, APP_DIR);
if (!appDir.exists()) {
appDir.mkdir();
}
recordButton = (Button) findViewById(R.id.audio_button);
textView = (TextView) findViewById(R.id.edit_text);
recordButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
record();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
return true;
} else {
return false;
}
}
});
}
private void record() {
final File audioSampleFile = new File(appDir, AUDIO_SAMPLE_NAME);
if (audioSampleFile.exists()) {
audioSampleFile.delete();
}
if (mediaRecorder == null) {
mediaRecorder = new MediaRecorder();
}
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(audioSampleFile.getPath());
mediaRecorder.start();
}
The following is the exception I get after application crashed in Phone
03-20 07:08:59.272: ERROR/NotificationService(243): Ignoring notification with icon==0: Notification(contentView=null vibrate=null,sound=nullnull,defaults=0x0,flags=0x62)
03-20 07:09:04.007: ERROR/MediaRecorder(7156): start called in an invalid state: 4
03-20 07:09:04.017: ERROR/AndroidRuntime(7156): FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:712)
at com.cs.gang.TestAudio.record(TestAudio.java:78)
where TestAudio.java:78 is the line
mediaRecorder.start();
I searched for this problem in other posts, but they don't seem to be exactly the same. I have permission
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and I believe I had correctly create file to access sd-card as well so what can be the cause of this problem or error? thanks
해결책
제휴하지 않습니다 StackOverflow