不支持的配置,采样率11025,格式1,ChannelMask 0x10

时间:2013-12-17 09:09:35

标签: android audio recorder

我正在申请中实施Audio Recorder。它工作了好几天,但它现在显示错误Unsupported Configuration,Sample rate 11025, format 1,ChannelMask 0x10.

这是我的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)

这是我实现录音的代码,

 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();
                  }



            } 

导致此错误的原因是什么?如何纠正?

1 个答案:

答案 0 :(得分:1)

您将无效(不受支持的)参数传递给getMinBufferSize(),并返回-2 ERROR_BAD_VALUE。尝试为数组分配负数元素会导致NegativeArraySizeException

现在,CHANNEL_CONFIGRATION_MONO已被弃用了很长时间。您应该使用CHANNEL_IN_MONO代替。这可能是ERROR_BAD_VALUE的原因。