AudioRecord - 空缓冲区

时间:2012-11-29 09:46:31

标签: android audiorecord

我正在尝试使用AudioRecord类录制一些音频。这是我的代码:

    int audioSource = AudioSource.MIC;
    int sampleRateInHz = 44100;
    int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSizeInShorts = 44100;
    int bufferSizeInBytes = 2*bufferSizeInShorts;

    short Data[] = new short[bufferSizeInShorts];
    Thread recordingThread;

    AudioRecord audioRecorder = new AudioRecord(audioSource,
                                                sampleRateInHz,
                                                channelConfig,
                                                audioFormat,
                                                bufferSizeInBytes);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }

        public void startRecording(View arg0) {
                audioRecorder.startRecording();
                recordingThread = new Thread(new Runnable() {
                    public void run() {
                        while (Data[bufferSizeInShorts-1] == 0)
                                audioRecorder.read(Data, 0, bufferSizeInShorts);
                    }
                });
                audioRecorder.stop();
        }

不幸的是,录制结束后我的短阵列是空的。我可以请你帮我弄清楚出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

我认为这段代码可以帮助您更好地使用所需的示例: - Link with Example