Pregunta

I am trying to read and understand audio in Android. In my search I came along this article. Here he has wrote a code to record audio in wav format. But there is one thing I don't fully understand, and that is the first loop of his code:

public class ExtAudioRecorder 
{
    private final static int[] sampleRates = {44100, 22050, 11025, 8000};

    public static ExtAudioRecorder getInstanse(Boolean recordingCompressed)
    {
        ExtAudioRecorder result = null;

        if(recordingCompressed)
        {
            result = new ExtAudioRecorder(  false, 
                                            AudioSource.MIC, 
                                            sampleRates[3], 
                                            AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                            AudioFormat.ENCODING_PCM_16BIT);
        }
        else
        {
            int i=0;
            do
            {
                result = new ExtAudioRecorder(  true, 
                                                AudioSource.MIC, 
                                                sampleRates[i], 
                                                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                                AudioFormat.ENCODING_PCM_16BIT);

            } while((++i<sampleRates.length) & !(result.getState() == ExtAudioRecorder.State.INITIALIZING));
        }
        return result;

He gives like a basic information about it, but I don't get this completely. Does this has anything to do with the performance of different types of Android devices? Anyway, hope somebody can brighten this up for me :)

¿Fue útil?

Solución

He is trying to initialize the audio recorder with different sample rates, from these {44100, 22050, 11025, 8000}.

Depending on the underlying hardware, not all sample rates may be supported by the device.

Although the documentation says:

"44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices."

I think the author has written code to make sure that if initialization at a sample rate fails, an attempt is made to initialize at some other sample rate, unless the initialization is successful, which is given by the check he is making in the loop condition.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top