AudioRecord not initialized

In the code below, my audioRecord not initialized. I tried moving it to the onCreate method and made it global. I registered a status and returns a value of 1 , which means readiness for use. The debugger says that startRecording is called in an uninitialized object. He also says that he cannot get a sound source.

Why am I getting these errors?

  package com.tecmark; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.TextView; public class recorder extends Activity { private Thread thread; private boolean isRecording; private AudioRecord recorder; private FileOutputStream os; private BufferedOutputStream bos; private DataOutputStream dos; private TextView text; private int audioSource = MediaRecorder.AudioSource.MIC; private int sampleRate = 22050; private int channel = AudioFormat.CHANNEL_CONFIGURATION_MONO; private int encoding = AudioFormat.ENCODING_PCM_16BIT; private int result = 0; private int bufferSize; private byte[] buffer; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v("onCreate", "layout set, about to init audiorec obj"); text = (TextView)findViewById(R.id.TextView01); bufferSize = AudioRecord.getMinBufferSize(sampleRate,channel,encoding); buffer = new byte[bufferSize]; recorder = new AudioRecord(audioSource, sampleRate,channel,encoding, AudioRecord.getMinBufferSize(sampleRate, channel,encoding)); Log.i("recorder obj state",""+recorder.getRecordingState()); } public void onClickPlay(View v){ } public void record(){ Log.i("inside record method", "******"); File path = Environment.getExternalStorageDirectory(); Log.v("file path", ""+path.getAbsolutePath()); File file = new File(path, "test.wav"); if(file.exists()){ file.delete(); } path.mkdirs(); Log.v("file path", ""+file.getAbsolutePath()); try { os = new FileOutputStream(file); bos = new BufferedOutputStream(os); dos = new DataOutputStream(bos); } catch (Exception e1) { e1.printStackTrace(); } int bufferSize = AudioRecord.getMinBufferSize(sampleRate,channel,encoding); byte[] buffer = new byte[bufferSize]; recorder.startRecording(); isRecording = true; try{ while (isRecording){ result = recorder.read(buffer, 0, bufferSize); for(int a=0; a<result;a++){ dos.write(buffer[a]); if(!isRecording){ recorder.stop(); break; } } } dos.flush(); dos.close(); }catch(Exception e){ e.printStackTrace(); } }// end of record method public void onClickStop(View v){ Log.v("onClickStop", "stop clicked"); isRecording=false; } public void onClickReverse(View v){ Log.v("onClickReverse", "reverse clicked"); } public void onClickRecord(View v){ Log.v("onClickRecourd", "record clicked, thread gona start"); text.setText("recording"); thread = new Thread(new Runnable() { public void run() { isRecording = true; record(); } }); thread.start(); isRecording = false; } }//end of class 

Logcat

 01-30 15:23:16.724: ERROR/AudioRecord(12817): Could not get audio input for record source 1 01-30 15:23:16.729: ERROR/AudioRecord-JNI(12817): Error creating AudioRecord instance: initialization check failed. 01-30 15:23:16.729: ERROR/AudioRecord-Java(12817): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object. 01-30 15:23:16.729: INFO/recorder obj state(12817): 1 01-30 15:23:16.729: WARN/dalvikvm(12817): threadid=13: thread exiting with uncaught exception (group=0x4001b180) 01-30 15:23:16.729: ERROR/AndroidRuntime(12817): Uncaught handler: thread Thread-7 exiting due to uncaught exception 01-30 15:23:16.739: ERROR/AndroidRuntime(12817): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. 01-30 15:23:16.739: ERROR/AndroidRuntime(12817): at android.media.AudioRecord.startRecording(AudioRecord.java:495) 01-30 15:23:16.739: ERROR/AndroidRuntime(12817): at com.tecmark.recorder.record(recorder.java:114) 01-30 15:23:16.739: ERROR/AndroidRuntime(12817): at com.tecmark.recorder$1.run(recorder.java:175) 01-30 15:23:16.739: ERROR/AndroidRuntime(12817): at java.lang.Thread.run(Thread.java:1096) 
+47
android android-audiorecord
Jan 30 '11 at 15:40
source share
12 answers

The trick using AudioRecord is that each device can have different initialization parameters, so you will need to create a method that combines all possible combinations of bit rates, encoding, etc.

 private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 }; public AudioRecord findAudioRecord() { for (int rate : mSampleRates) { for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) { for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) { try { Log.d(C.TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + channelConfig); int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat); if (bufferSize != AudioRecord.ERROR_BAD_VALUE) { // check if we can instantiate and have a success AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize); if (recorder.getState() == AudioRecord.STATE_INITIALIZED) return recorder; } } catch (Exception e) { Log.e(C.TAG, rate + "Exception, keep trying.",e); } } } } return null; } AudioRecord recorder = findAudioRecord(); recorder.release(); 
+82
Mar 26 '11 at 5:30
source share

I had the same problem, it was solved by posting

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

in the manifest.

Since Lollipop, you also need to specifically ask the user about each permission. Because they may have recalled them. Make sure permission is granted.

+83
May 11 '12 at 1:48
source share

According to javadocs, all devices are guaranteed to support this format (for recording):

44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT.

Go to CHANNEL_OUT_MONO to play.

+9
Jul 15 2018-11-11T00:
source share

Now, with the candy, you need to specifically ask the user about each permission. Make sure permission is granted.

+4
Apr 19 '16 at 3:02
source share

The problem with initializing multiple AudioRecord objects can be fixed using audioRecord.release(); before creating the next object ... Read more here: Android AudioRecord - will not initialize a second time

+3
Oct 31 '12 at 21:13
source share

Even after doing all the above steps, I was getting the same problem that worked for me, is that my os was marshmallow and I had to ask for permission .

+3
Nov 06 '16 at 4:34
source share

Just the same problem. The solution was to restart the device. While playing with the code, I did not release the AudioRecord object, which clearly made the audio device stuck. To check if the audio device is working or not, I downloaded Audalyzer from Google Play.

+2
Jul 15 '12 at 10:52
source share

If your mobile phone system is Android M or higher, you may need to apply the audio recording resolution in Android M. http://developer.android.com/guide/topics/security/permissions.html

+2
May 6 '16 at 7:46 a.m.
source share

I noticed that when the SDCard on avd that I run fills up, the AudioRecord constructor returns null. Have you tried to clear the sdcard?

+1
Apr 19 2018-11-11T00:
source share

I think this is due to the stream, not knowing that you paused the main action and are still trying to record after you stopped the recorder.

I solved this by changing the onResume () and onPause () methods to change the boolean value of isRecording.

 public void onResume() { ... isRecording = true; } public void onPause() { ... isRecording = false; } 

Then, in your thread, combine both startRecording() and stop() with if-statements checking isRecording:

 if(isRecording) recorder.startRecording(); ... if(isRecording) recorder.stop(); // which you've done 
0
Jun 18 '13 at 15:52
source share

I rewrote the answer from @DustinB for those using Xamarin Android AudioRecord with C #.

 int[] sampleRates = new int[] { 44100, 22050, 11025, 8000 }; Encoding[] encodings = new Encoding[] { Encoding.Pcm8bit, Encoding.Pcm16bit }; ChannelIn[] channelConfigs = new ChannelIn[]{ ChannelIn.Mono, ChannelIn.Stereo }; //Not all of the formats are supported on each device foreach (int sampleRate in sampleRates) { foreach (Encoding encoding in encodings) { foreach (ChannelIn channelConfig in channelConfigs) { try { Console.WriteLine("Attempting rate " + sampleRate + "Hz, bits: " + encoding + ", channel: " + channelConfig); int bufferSize = AudioRecord.GetMinBufferSize(sampleRate, channelConfig, encoding); if (bufferSize > 0) { // check if we can instantiate and have a success AudioRecord recorder = new AudioRecord(AudioSource.Mic, sampleRate, channelConfig, encoding, bufferSize); if (recorder.State == State.Initialized) { mBufferSize = bufferSize; mSampleRate = sampleRate; mChannelConfig = channelConfig; mEncoding = encoding; recorder.Release(); recorder = null; return true; } } } catch (Exception ex) { Console.WriteLine(sampleRate + "Exception, keep trying." + ex.Message); } } } } 
0
Feb 22 '15 at 8:48
source share

in my case, I had to manually enable the resolution in android 7 for the microphone, as sean zhu commented.

0
Nov 03 '17 at 20:46 on
source share



All Articles