Forced error due to record recorder.setAudioSource (MediaRecorder.AudioSource.MIC);

I create an application for a sound recorder, I work all day on the Internet, and I found some codes that can be used to record sound. but every time I run the code, my application gets into a power error and shuts down. The code I found is as follows

    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 am using Android API 2.1

+3
source share
3 answers

The best example you can find here: http://developer.android.com/guide/topics/media/index.html#capture

I hope you tested it on the phone because:

, , , , , MediaRecorder.

:

/*
 * 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 AndroidManifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.RECORD_AUDIO" />
 */
+7

 mRecorder = new MediaRecorder();
 mRecorder.reset();
 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

question, .

0

Grant permission in manifest file: -

<uses-permission android:name="android.permission.RECORD_AUDIO" />
0
source

Source: https://habr.com/ru/post/1796644/


All Articles