1

I' m trying to read recorded data into the short array. I want to continue recording and reading until it is full. My code: http://pastebin.com/r6yuPn82 Unfortunately applications crashes after calling startRecording method. Errors: http://pastebin.com/9jwrPLNc May I kindly ask you to help me fixing it?

Arxas
  • 241
  • 2
  • 4
  • 13

2 Answers2

3

Your AudioRecord hasn't been initialized correctly.

"Caused by: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord."

Put this in your manifest:

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
Siggy
  • 752
  • 1
  • 5
  • 26
  • 2
    Thanks, it's not crashing anymore. However I checked content of my short array and it's filled with zeros. What I did wrong? – Arxas Nov 29 '12 at 08:09
  • In the documentary [link](http://developer.android.com/reference/android/media/AudioRecord.html) it says: `public int read (ByteBuffer audioBuffer, int sizeInBytes)` "Added in API level 3 Reads audio data from the audio hardware for recording into a direct buffer. If this buffer is not a direct buffer, this method will always return 0." I think you need a ByteBuffer instead of your short Array. – Siggy Nov 29 '12 at 08:14
  • According to the documentary I can also use short type: read(short[] audioData, int offsetInShorts, int sizeInShorts) Reads audio data from the audio hardware for recording into a buffer. – Arxas Nov 29 '12 at 08:16
  • I'm using 16-bit PCM so every sample needs 2 bytes. – Arxas Nov 29 '12 at 08:22
  • Yeah sorry my mistake. I currently have no idea, sorry :/ Maybe ask this in a new question so it gets some more attention. But maybe the first answer of this question helps: [link](http://stackoverflow.com/questions/4707994/android-audiorecord-questions) – Siggy Nov 29 '12 at 08:33
  • I tried to use ByteBuffer first and then copy data into byte array but it's still filled with zeros :/ Anyway thank you for your help, at least I can try to do something now instead of having my app crashed every time. – Arxas Nov 29 '12 at 08:44
  • 1
    @Siggy, did you post a new question on this somewhere? I'm having an issue where the shorts I read into the buffer are all 0 (zero) so I'd like to see if you solved this. – marienke Mar 06 '13 at 09:16
  • I solved the "all zero" problem by changing my inputs -- I was not using the microphone. – bradreaves Nov 02 '16 at 04:00
1

Make sure that you're recording before you read from your MIC to the buffer.

See answer in this post saying the the following:

 short[] audioData = new short[bufferSize];

 int offset =0;
 int shortRead = 0;

 //start tapping into the microphone
 audioRecored.startRecording();

 //start reading from the microphone to an internal buffer - chuck by chunk
 while (offset < bufferSize)
 {
    shortRead = audioRecored.read(audioData, offset ,bufferSize - offset);
        offset += shortRead;
 }

 //stop tapping into the microphone
 audioRecored.stop();
Community
  • 1
  • 1
marienke
  • 2,465
  • 4
  • 34
  • 66