• android AudioRecorder简单心得


    1.如何创建一个有效的AudioRecorder实例

    Android各种设备的采样频率不同,输入的声道数也不同,如果采用固定的采样频率和声道数,那么得到的AudioRecorder不一定能够正常初始化。

    为了正常使用,需要尝试各种不同的参数,得到在此设备上可以用的AudioRecorder实例。代码如下:


        private void createAudioRecord() {
                   for (int sampleRate : new int[]{44100, 8000, 11025, 16000, 22050, 32000,
                    47250, 48000}) {
                for (short audioFormat : new short[]{
                        AudioFormat.ENCODING_PCM_16BIT,
                        AudioFormat.ENCODING_PCM_8BIT}) {
                    for (short channelConfig : new short[]{
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.CHANNEL_IN_STEREO}) {
    
                        // Try to initialize
                        try {
                            recBufSize = AudioRecord.getMinBufferSize(sampleRate,
                                    channelConfig, audioFormat);
    
                            if (recBufSize < 0) {
                                continue;
                            }
    
                            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                    sampleRate, channelConfig, audioFormat,
                                    recBufSize * 2);
    
                            if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
                                
                                return;
                            }
    
                            audioRecord.release();
                            audioRecord = null;
                        } catch (Exception e) {
                            // Do nothing
                        }
                    }
                }
            }
    
            throw new IllegalStateException(
                    "getInstance() failed : no suitable audio configurations on this device.");
        }

    2.常见错误

    1.有些设备上面,即使你得到了有效的AudioRecorder实例,在audioRecord.startRecording()的时候还会报ERROR_BAD_VALUE错误。
    这有可能是你使用了AudioManager而没有释放导致的。
    其他错误都可以在网络上找到答案。



  • 相关阅读:
    vue全局启用 emulateJSON 选项
    vue全局配置数据接口的根域名
    CSS实现按钮YES-NO按钮+Jquery获取按钮状态。
    Redis命令
    在js中获取 input checkbox里选中的多个值
    Python中常见字符串去除空格的方法总结
    e.target.value和this的区别
    用脚本来运行scrapy crawl ...
    生成器的两种方式
    python中ord()函数,chr()函数,unichr()函数
  • 原文地址:https://www.cnblogs.com/james1207/p/3333765.html
Copyright © 2020-2023  润新知