Forceclose error due to recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
I am creating a sound recorder app, I've been googling the whole day and I've found a few codes that can be used to record audio. but every time I run the code my application hits a force close error and shuts down. The code I've found is as follow
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); ***<== ERROR***
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/sample.3gp");
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(i开发者_如何学PythonnfoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I'm using Android API 2.1
The best example you can find is here: http://developer.android.com/guide/topics/media/index.html#capture
I hope you tested it on a phone, because:
Note that the emulator doesn't have hardware to capture audio or video, but actual mobile devices are likely to provide these capabilities, accessible through the MediaRecorder class.
And have a look at this:
/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*/
Provide permission in Manifest file:-
<uses-permission android:name="android.permission.RECORD_AUDIO" />
This worked for me
mRecorder = new MediaRecorder();
mRecorder.reset();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
This is same question and I wrote the same answer there.
精彩评论