Why does the start () method of MediaRecorder throw an IllegalStateException?

I'm trying to record a sound, but the start() method of the MediaRecorder class throws an IllegalStateException . I am using the following code:

 MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile("/sdcard/"); try { recorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("Try","Exception"); recorder.start(); 

And the next resolution

 <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
+7
source share
2 answers

Vijay, recorder.setOutputFile ("/ sdcard /"); sets the directory, not the file. Replace it as follows:

 mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName += "/youraudiofile.3gp"; 

Using hardcodes "/ sdcard" is a fragile way, so use the above

In addition, for this you need to add

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

to your AndroidManifext.xml

+13
source

This may be useful for some in the future. IllegalstateException is MediaRecorder. Prepare when the MediaRecorder. Prepare method MediaRecorder. Prepare MediaRecorder. Prepare not called or not called after Mediarecorder.start or before setting up audio / video sources, format and encoders. The correct configuration order is mentioned in the camera’s developer guide in the Android documentation.

  • camera unlock
  • control camera on media β†’ setCamera
  • set audio / video source, format, encoder
  • to prepare
  • start
+4
source

All Articles