3

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(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

I'm using Android API 2.1

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Umer Hassam
  • 1,332
  • 5
  • 23
  • 42
  • Please post the error in the stack trace. You can find it in the LogCat view of eclipse or by running the `adb logcat` by yourself... – WarrenFaith Mar 06 '11 at 14:58
  • Possible duplicate of [android mediaRecorder.setAudioSource failed](https://stackoverflow.com/questions/3782786/android-mediarecorder-setaudiosource-failed) – Kalle Richter Mar 10 '18 at 13:24

3 Answers3

8

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" />
 */
Elepferd
  • 156
  • 4
  • Thnkyou very much, setting the permissions did the trick... the code doesn't show any errors however i dont know where the file that i recorded is stored or is it even being recorded at all.... any idea where "/sdcard/sample.3gp" would be,, and another thing im a student so i don't have an android mobile i can only test it on the emulator... any help would be greatly appreciated ... thnks in advance... – Umer Hassam Mar 06 '11 at 20:06
  • On a phone you can access the sdcard with a File-Explorer (there are lots of apps in the market), currently I have no idea how this is simulated in the emulator. I see no way to test the recording without a phone at the moment. – Elepferd Mar 06 '11 at 22:16
0

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Ali Riahipour
  • 524
  • 6
  • 20
0

Provide permission in Manifest file:-

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Appnweb31
  • 74
  • 3