Domanda

I am using the below code to get the valid sample rate for the device but it always sends 0 for audioRecorder.getState() for My Samsung Galaxy Tab 1 OS version 2.3.6 but its fine for Samsung Galaxy S2.

Means for Galaxy Tab I always get desiredRate = -1;

public static void getValidSampleRates() {
        int desiredRate = 0;
        for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) {  // add the rates you wish to check against
            int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
            if (bufferSize > 0) {
                // buffer size is valid, Sample rate supported
                AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 21168);

                if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
                    desiredRate = -1;
                else
                    desiredRate = rate;
            }
        }
    }

I have added the below permissions in Android manifest file

 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application

please help

È stato utile?

Soluzione

I see two issues here:

  1. You are using a hardcoded value for bufferSizeInBytes. According to the documentation, if you use a value smaller than getMinBufferSize will result in an initialization failure. Why don't you use your value of bufferSize in the constructor? Also, use the same value of channelConfig when calling getMinBufferSize.

  2. Similar to the answer in this question, someone may have not released the AudioRecord resources. You could make some changes to your if statement as shown in the code block below. If this is true, then your code will fail while trying to initialize a bit-rate of 44100, release the resources correctly and then successfully initialize a bit-rate of 8000. If you still want to have a bit-rate of 44100, use new int[] {44100, 44100, 8000, 11025, 16000, 22050}

The code block below includes both changes to your code. Please try it and let me know what you get.

public static void getValidSampleRates() {
    int desiredRate = 0;
    for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) {
        int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        if (bufferSize > 0) {
            // buffer size is valid, Sample rate supported
            AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

            if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                desiredRate = -1;
                audioRecorder.release();
            } else {
                desiredRate = rate;
                break;
            }
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top