AudioRecord object not initialized in project

When I try to record audio through my Nexus 5 using the following code

record = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT, BYTE_OF_SAMPLE * bufferSampleSize); record.startRecording(); 

Then I get the following exceptions in logcat:

 E/AudioRecord: AudioFlinger could not create record track, status: -1 E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. E/AndroidRuntime: FATAL EXCEPTION: Thread-855 E/AndroidRuntime: Process: com.*.*, PID: 14206 E/AndroidRuntime: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. E/AndroidRuntime: at android.media.AudioRecord.startRecording(AudioRecord.java:885) E/AndroidRuntime: at com.*.*.MainActivity$Looper.run(MainActivity.java:265) E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9e476d00 

I added the following line to the manifest file.

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

I tested an AudioRecord object not initializing . None of these solutions work.

But his work is in another project. Is something missing?

+6
source share
2 answers

Solution received: Recently, I updated my phone to Marshmallow. Zephyr has a new permission system. I have included these permissions because the recording works very well.

enter image description here

+11
source

I used this for recording. find the full code from here Android audio file recording example

 private void startRecording() { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC); mRecorder.setAudioEncodingBitRate(48000); } else { mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); mRecorder.setAudioEncodingBitRate(64000); } mRecorder.setAudioSamplingRate(16000); mOutputFile = getOutputFile(); mOutputFile.getParentFile().mkdirs(); mRecorder.setOutputFile(mOutputFile.getAbsolutePath()); try { mRecorder.prepare(); mRecorder.start(); mStartTime = SystemClock.elapsedRealtime(); mHandler.postDelayed(mTickExecutor, 100); Log.d("Voice Recorder","started recording to "+mOutputFile.getAbsolutePath()); } catch (IOException e) { Log.e("Voice Recorder", "prepare() failed "+e.getMessage()); } } 

Android audio recording

+1
source

All Articles