其他分享
首页 > 其他分享> > AudioRecord.State is always State.Uninitialized

AudioRecord.State is always State.Uninitialized

作者:互联网

今天在自己的锤子手机上遇到个奇葩问题,AudioRecord初始化失败,其他手机都正常的。
一通google下来,找到了个折中办法,就是把所有AudioSource音频来源,采样率,采样精度这些都遍历一遍。方法见以下代码。

//音频采集来源
private static final int mAudioSource = MediaRecorder.AudioSource.MIC; //麦克风
//音频采样率 (MediaRecoder的采样率通常是8000Hz AAC的通常是44100Hz.设置采样率为44100目前为常用的采样率,官方文档表示这个值可以兼容所有的设置)
private static final int mSampleRateInHz = 16000/*44100*/;
//声道
private static final int mChannelConfig = AudioFormat.CHANNEL_IN_MONO; //单声道
//数据格式  (指定采样的数据的格式和每次采样的大小)
//指定音频量化位数 ,在AudioFormaat类中指定了以下各种可能的常量。通常我们选择ENCODING_PCM_16BIT和ENCODING_PCM_8BIT PCM代表的是脉冲编码调制,它实际上是原始音频样本。
//因此可以设置每个样本的分辨率为16位或者8位,16位将占用更多的空间和处理能力,表示的音频也更加接近真实。
private static final int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
//指定最小录音缓冲区大小
private int mBufferSizeInBytes;
//计算最小录音缓冲区大小
mBufferSizeInBytes = AudioRecord.getMinBufferSize(mSampleRateInHz, mChannelConfig, mAudioFormat);
AudioRecord mAudioRecord = new AudioRecord(mAudioSource, mSampleRateInHz, mChannelConfig, mAudioFormat, mBufferSizeInBytes);

private static int[] mSampleRates = new int[]{8000, 16000,11025, 22050, 44100};
private static int[] mAudioSourceArr = new int[]{MediaRecorder.AudioSource.DEFAULT,
        MediaRecorder.AudioSource.MIC};
private static int[] mAudioFormataArr = new int[]{
        AudioFormat.ENCODING_INVALID,
        AudioFormat.ENCODING_DEFAULT,
        AudioFormat.ENCODING_PCM_16BIT,
        AudioFormat.ENCODING_PCM_8BIT,
        AudioFormat.ENCODING_PCM_FLOAT,
        AudioFormat.ENCODING_AC3,
        AudioFormat.ENCODING_E_AC3,
        AudioFormat.ENCODING_DTS,
        AudioFormat.ENCODING_DTS_HD,
        AudioFormat.ENCODING_MP3,
        AudioFormat.ENCODING_AAC_LC,
        AudioFormat.ENCODING_AAC_HE_V1,
        AudioFormat.ENCODING_AAC_HE_V2,
        AudioFormat.ENCODING_IEC61937,
        AudioFormat.ENCODING_DOLBY_TRUEHD,
        AudioFormat.ENCODING_AAC_XHE,
        AudioFormat.ENCODING_AC4,
        AudioFormat.ENCODING_E_AC3_JOC};
private static int[] mChannelConfigArr = new int[]{
        AudioFormat.CHANNEL_IN_DEFAULT,
        AudioFormat.CHANNEL_IN_RIGHT,
        AudioFormat.CHANNEL_IN_FRONT,
        AudioFormat.CHANNEL_IN_BACK,
        AudioFormat.CHANNEL_IN_LEFT_PROCESSED,
        AudioFormat.CHANNEL_IN_RIGHT_PROCESSED,
        AudioFormat.CHANNEL_IN_FRONT_PROCESSED,
        AudioFormat.CHANNEL_IN_BACK_PROCESSED,
        AudioFormat.CHANNEL_IN_PRESSURE,
        AudioFormat.CHANNEL_IN_X_AXIS,
        AudioFormat.CHANNEL_IN_Y_AXIS,
        AudioFormat.CHANNEL_IN_Z_AXIS,
        AudioFormat.CHANNEL_IN_VOICE_UPLINK,
        AudioFormat.CHANNEL_IN_VOICE_DNLINK,
        AudioFormat.CHANNEL_IN_MONO,
        AudioFormat.CHANNEL_IN_STEREO,
};

public AudioRecord findAudioRecord() {

    for (int rate : mSampleRates) {
        // AudioFormat.ENCODING_PCM_16BIT;
        for (int audioFormat : mAudioFormataArr) {
            for (int channelConfig : mChannelConfigArr) {
                for (int mAudioSource : mAudioSourceArr) {
                    try {
                        int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                        if (bufferSize > 0) {
                            // check if we can instantiate and have a success
                            AudioRecord recorder = new AudioRecord(mAudioSource, rate, channelConfig, audioFormat, bufferSize);

                            int state = recorder.getState();
                            LogUtil.i("state:" + state);
                            if (state == AudioRecord.STATE_INITIALIZED) {
                                LogUtil.i("audioformat:" + audioFormat + ";channelConfig:" + channelConfig + ";mAudioSource:" + mAudioSource);
                                //audioformat:2;channelConfig:1;mAudioSource:0
                                //ENCODING_PCM_16BIT
                                //CHANNEL_IN_DEFAULT
                                //DEFAULT
                                return recorder; //It never gets here ?????
                            } else {
                                recorder.release();
                            }
                        }

                    } catch (Exception e) {
                        LogUtil.i("e:" + e.getLocalizedMessage());
                    }
                }


            }

        }

    }

    return null;
}

 

标签:Uninitialized,ENCODING,int,always,AudioRecord,State,PCM,AudioFormat,CHANNEL
来源: https://blog.csdn.net/oShenLi1/article/details/111059447