不支持的配置,采样率11025,格式1,信道掩码0x10



我正在应用程序中实现录音机。它工作了几天很好,但现在显示错误Unsupported Configuration,Sample rate 11025, format 1,ChannelMask 0x10.

这是我的日志:

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

            } 

是什么导致了这个错误,我该如何纠正?

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

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

最新更新