My code to take audio recorder.

Anything I missing?


I know following code is not upto mark but I tried to extract what I have in 
the code.



public static AudioRecord findAudioRecord(Context context) {
    AudioRecord recorder = null;
    try {
        AudioManager audioManager = (AudioManager) 
context.getSystemService(Context.AUDIO_SERVICE);
        int _rate = 
Integer.parseInt(audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
        //int bufferSize = 
Integer.parseInt(audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
        for (short audioFormat : new short[]{AudioFormat.ENCODING_PCM_8BIT, 
AudioFormat.ENCODING_PCM_16BIT}) {
            for (short channelConfig : new short[]{AudioFormat.CHANNEL_IN_MONO, 
AudioFormat.CHANNEL_IN_STEREO}) {

                Log.d(TAG, "Attempting rate " + _rate + "Hz, bits: " + 
audioFormat + ", channel: "
                        + channelConfig);
                int bufferSize = AudioRecord.getMinBufferSize(_rate, 
channelConfig, audioFormat);
                if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                    // check if we can instantiate and have a success
                    recorder = new 
AudioRecord(MediaRecorder.AudioSource.DEFAULT, _rate, channelConfig, 
audioFormat, bufferSize);

                    if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
                        _bufferSize = bufferSize;
                        return recorder;
                    }
                }

            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception, keep trying.", e);
    }

    // I'm removing above configurations (_rate) from below code...


    for (int rate : new int[]{8000, 11025, 16000, 22050, 44100}) {
        for (short audioFormat : new short[]{AudioFormat.ENCODING_PCM_8BIT, 
AudioFormat.ENCODING_PCM_16BIT}) {
            for (short channelConfig : new short[]{AudioFormat.CHANNEL_IN_MONO, 
AudioFormat.CHANNEL_IN_STEREO}) {
                try {
                    Log.d(TAG, "Attempting rate " + rate + "Hz, bits: " + 
audioFormat + ", channel: "
                            + channelConfig);
                    bufferSize = AudioRecord.getMinBufferSize(rate, 
channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        recorder = new 
AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, 
audioFormat, bufferSize);

                        if (recorder.getState() == 
AudioRecord.STATE_INITIALIZED) {
                            _bufferSize = bufferSize;
                            return recorder;
                        }
                    }
                } catch (Exception e) {
                    Log.e(TAG, rate + "Exception, keep trying.", e);
                }
            }
        }
    }
    return null;
}





-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/7fda962f-85b5-4adc-85d6-5313af2201a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to