Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.media.AudioRecord;

public class Main {
    /**
     * Check correct buffer size for your AudioRecord instance
     *
     * @param audioSource
     *     the audio source
     * @param fs
     *     the fs
     * @param channelConfiguration
     *     the channel configuration
     * @param audioEncoding
     *     the audio encoding
     * @return the int
     */
    public static int getValidBufferSize(int audioSource, int fs, int channelConfiguration, int audioEncoding) {
        for (int bufferSize : new int[] { 256, 512, 1024, 2048, 4096 }) { // add the rates you wish to check against
            AudioRecord audioRecordTemp = new AudioRecord(audioSource, fs, channelConfiguration, audioEncoding,
                    bufferSize);
            if (audioRecordTemp != null && audioRecordTemp.getState() == AudioRecord.STATE_INITIALIZED) {
                return bufferSize;
            }
        }
        return 0;
    }
}