AudioRecord - empty buffer

Posted by Arxas on Stack Overflow See other posts from Stack Overflow or by Arxas
Published on 2012-11-29T09:46:30Z Indexed on 2012/11/29 17:04 UTC
Read the original article Hit count: 186

Filed under:
|

I' m trying to record some audio using AudioRecord class. Here is my code:

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

Unfortunately my short array is empty after the recording is over. May I kindly ask you to help me figure out what's wrong?

© Stack Overflow or respective owner

Related posts about android

Related posts about audiorecord