Note that there are some explanatory texts on larger screens.

plurals
  1. POErrors when recording sound in Android
    text
    copied!<p>I have made an app that records sound and analyses it for frequency. This process is repeated a couple of times every second and thus uses threading. </p> <p>This does work most of the time, but for some reason in the logcat I get these messages repeated after the first analysis.</p> <p>Rarely (but sometimes) when I test, the app records no sound. So I'm thinking it has something to do with this error.</p> <pre><code>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. </code></pre> <p>The code is below, does anyone have any idea where im going wrong? Am I not killing the AudioRecord object correctly? Code has been modifed for ease of reading:</p> <pre><code>public class recorderThread extends AsyncTask&lt;Sprite, Void, Integer&gt; { 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload