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

标签 android audio recorder

我在我的应用程序中实现了录音机。它可以正常工作几天,但是现在显示错误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的原因。

关于android - 不支持的配置,采样率11025,格式1,ChannelMask 0x10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20630009/

相关文章:

java - 显示位图图像 (ImageView)

ios - 使用音频单元播放音频文件?

Android:使用默认录像机录制并返回sd卡路径

windows-7 - screen_capture_recorder 记录第二个监视器 ffmpeg Win 7

html - 有没有办法在 rails/web/html5 上使用 ruby​​ 在 mp4 中录制音频

java - 在游戏控制台 kotlin.UninitializedPropertyAccessException 中出现此错误

android - firebase 信息/连接 onDataChange 1 分钟 false 无响应

android - 使用图形加速运行 Android 模拟器

audio - 音频DSP初学者的资源?

python - 如何在 ssh session 中发出铃声?