Android AudioRecord Supported Sampling Rates

前端 未结 8 696
悲&欢浪女
悲&欢浪女 2020-11-28 22:47

I\'m trying to figure out what sampling rates are supported for phones running Android 2.2 and greater. We\'d like to sample at a rate lower than 44.1kHz and not have to re

8条回答
  •  佛祖请我去吃肉
    2020-11-28 23:23

    This method gives the minimum audio sample rate supported by your device.

    NOTE : You may reverse the for loop to get the maximum sample rate supported by your device (Don't forget to change the method name).

    NOTE 2 : Though android doc says upto 48000(48khz) sample rate is supported ,I have added all the possible sampling rates (as in wikipedia) since who know new devices may record UHD audio in higher (sampling) framerates.

    private int getMinSupportedSampleRate() {
        /*
         * Valid Audio Sample rates
         * 
         * @see Wikipedia
         */
        final int validSampleRates[] = new int[] { 8000, 11025, 16000, 22050,
                32000, 37800, 44056, 44100, 47250, 48000, 50000, 50400, 88200,
                96000, 176400, 192000, 352800, 2822400, 5644800 };
        /*
         * Selecting default audio input source for recording since
         * AudioFormat.CHANNEL_CONFIGURATION_DEFAULT is deprecated and selecting
         * default encoding format.
         */
        for (int i = 0; i < validSampleRates.length; i++) {
            int result = AudioRecord.getMinBufferSize(validSampleRates[i],
                    AudioFormat.CHANNEL_IN_DEFAULT,
                    AudioFormat.ENCODING_DEFAULT);
            if (result != AudioRecord.ERROR
                    && result != AudioRecord.ERROR_BAD_VALUE && result > 0) {
                // return the mininum supported audio sample rate
                return validSampleRates[i];
            }
        }
        // If none of the sample rates are supported return -1 handle it in
        // calling method
        return -1;
    }
    

提交回复
热议问题