Errors when recording sound in Android

I made an application that records sound and analyzes it for frequency. This process is repeated a couple of times per second and thus uses threads.

This works most of the time, but for some reason in logcat I get these messages after the first analysis.

Rarely (but sometimes) when I test, the application does not record sound. So I think this has something to do with this error.

01-23 13:52:03.414: E/AudioRecord(3647): Could not get audio input for record source 1 01-23 13:52:03.424: E/AudioRecord-JNI(3647): Error creating AudioRecord instance: initialization check failed. 01-23 13:52:03.424: E/AudioRecord-Java(3647): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object. 

Below is the code, does anyone have any ideas where I am going wrong? I am not killing the AudioRecord object correctly? The code has been modified for readability:

 public class recorderThread extends AsyncTask<Sprite, Void, Integer> { short[] audioData; int bufferSize; @Override protected Integer doInBackground(Sprite... ball) { boolean recorded = false; int sampleRate = 8192; AudioRecord recorder = instatiateRecorder(sampleRate); while (!recorded) { //loop until recording is running if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet. { if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED) recorder.startRecording(); //check to see if the Recorder has stopped or is not recording, and make it record. else { //read the PCM audio data into the audioData array //get frequency //checks if correct frequency, assigns number int correctNo = correctNumber(frequency, note); checkIfMultipleNotes(correctNo, max_index, frequency, sampleRate, magnitude, note); if (audioDataIsNotEmpty()) recorded = true; return correctNo; } } else { recorded = false; recorder = instatiateRecorder(sampleRate); } } if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) { killRecorder(recorder); } return 1; } private void killRecorder(AudioRecord recorder) { recorder.stop(); //stop the recorder before ending the thread recorder.release(); //release the recorders resources recorder=null; //set the recorder to be garbage collected } @Override protected void onPostExecute(Integer result) { ballComp.hitCorrectNote = result; } private AudioRecord instatiateRecorder(int sampleRate) { bufferSize= AudioRecord.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT)*2; //get the buffer size to use with this audio record AudioRecord recorder = new AudioRecord (AudioSource.MIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder audioData = new short [bufferSize]; //short array that pcm data is put into. return recorder; } } 
+4
source share
3 answers

As your journal says it means "Failed to get audio input for recording source 1". The Android device did not detect any sound recording equipment.

So, if you are testing the application from the emulator, make sure that you have successfully connected the mice during sound recording, or if you are debugging or running it from the device, then make sure the microphone is turned on to record sound.

Hope this helps you.

Or

If the above does not solve your problem, use the code below to record sound, as it works great for me.

code:

  record.setOnClickListener(new View.OnClickListener() { boolean mStartRecording = true; public void onClick(View v) { if (mStartRecording==true) { //startRecording(); haveStartRecord=true; String recordWord = wordValue.getText().toString(); String file = Environment.getExternalStorageDirectory().getAbsolutePath(); file = file+"/"+recordWord+".3gp"; System.out.println("Recording Start"); //record.setText("Stop recording"); record.setBackgroundDrawable(getResources().getDrawable( R.drawable.rec_on)); mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(file); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

//mRecorder.setAudioChannels(1); //mRecorder.setAudioSamplingRate(8000);

  try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); } else { //stopRecording(); System.out.println("Recording Stop"); record.setBackgroundDrawable(getResources().getDrawable( R.drawable.rec_off)); mRecorder.stop(); mRecorder.release(); mRecorder = null; haveFinishRecord=true; } mStartRecording = !mStartRecording; } }); 

Hope this answer helps you.

Enjoy. :)

+2
source

What stops you at the same time with two RecorderThreads programs? Show the code that creates an instance of one of these objects, execute it, and, of course, expect the previous RecorderThread to finish first.

If the answer is that nothing stops the two starts of RecorderThreads at the same time, then your use of “static” will obviously be a problem ... the second thread will leak the first AudioRecord when it opens. IMHO, it is a good idea to try to avoid using static data.

+1
source

I had the same problem. And I solved it by adding

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

in "AndroidManifest.xml"

+1
source

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


All Articles