在Android中获取AudioRecord(Samsung Young)null

时间:2013-11-23 18:17:38

标签: android samsung-mobile

我想捕获音频,所以我正在使用AudioRecord。但是我在AudioRecord中得到了null。请帮助我,使其适用于所有手机。

private static int[] mSampleRates = new int[] { 44100, 44056, 47250, 48000, 22050, 16000, 11025, 8000 };


public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        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(LOG_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
                        AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

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

的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

1 个答案:

答案 0 :(得分:0)

你有吗

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
在你的清单中