Question

I am implementing Audio Recorder in my application. It worked fine for a few days but it now shows the error Unsupported Configuration,Sample rate 11025, format 1,ChannelMask 0x10.

This is my logcat:

 12-17 09:04:26.325: E/AudioRecord(1195): Unsupported configuration: sampleRate 11025, format 1, channelMask 0x10
 12-17 09:04:26.335: W/dalvikvm(1195): threadid=17: thread exiting with uncaught exception (group=0x40a71930)
 12-17 09:04:26.385: E/AndroidRuntime(1195): FATAL EXCEPTION: Thread-117
 12-17 09:04:26.385: E/AndroidRuntime(1195): java.lang.NegativeArraySizeException: -2
 12-17 09:04:26.385: E/AndroidRuntime(1195):    at com.example.sms.OtherActivity.startRecord(OtherActivity.java:196)
 12-17 09:04:26.385: E/AndroidRuntime(1195):    at com.example.sms.OtherActivity.access$5(OtherActivity.java:180)
 12-17 09:04:26.385: E/AndroidRuntime(1195):    at com.example.sms.OtherActivity$1$1.run(OtherActivity.java:142)
 12-17 09:04:26.385: E/AndroidRuntime(1195):    at java.lang.Thread.run(Thread.java:856)

This is my code for implementing audio record,

 private void startRecord()
            { 
                  File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

                  try
                  {
                        file.createNewFile();

                        OutputStream outputStream = new FileOutputStream(file);
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                        DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);

                        int minBufferSize = AudioRecord.getMinBufferSize(11025,
                                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT);

                        short[] audioData = new short[minBufferSize];

                        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                    11025,
                                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT,
                                    minBufferSize);

                        audioRecord.startRecording();

                        while(recording)
                        {
                              int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
                              for(int i = 0; i < numberOfShort; i++)
                              {
                                    dataOutputStream.writeShort(audioData[i]);
                              }
                        }
                        audioRecord.stop();
                        dataOutputStream.close();

                  }
                  catch (IOException e)
                  {
                        e.printStackTrace();
                  }

            }

            void playRecord()
            {
                  File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

                  int shortSizeInBytes = Short.SIZE/Byte.SIZE;

                  int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
                  short[] audioData = new short[bufferSizeInBytes];

                  try {
                        InputStream inputStream = new FileInputStream(file);
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                        DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

                        int i = 0;
                        while(dataInputStream.available() > 0)
                        {
                              audioData[i] = dataInputStream.readShort();
                              i++;
                        }

                        dataInputStream.close();

                        AudioTrack audioTrack = new AudioTrack(
                                    AudioManager.STREAM_MUSIC,
                                    11025,
                                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT,
                                    bufferSizeInBytes,
                                    AudioTrack.MODE_STREAM);

                        audioTrack.play();
                        audioTrack.write(audioData, 0, bufferSizeInBytes);


                  } catch (FileNotFoundException e)
                  {
                        e.printStackTrace();
                  } catch (IOException e)
                  {
                        e.printStackTrace();
                  }



            } 

What is causing this error and how can I rectify it?

Was it helpful?

Solution

You are passing invalid (unsupported) params to getMinBufferSize() and it returns -2 ERROR_BAD_VALUE. Attempting to allocate negative number of elements for an array causes the NegativeArraySizeException.

Now, CHANNEL_CONFIGRATION_MONO has been deprecated for a long time. You should use CHANNEL_IN_MONO instead. That's the likely cause for the ERROR_BAD_VALUE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top