使用AVAudioRecorder录音保存.wav文件的坑

1,811 阅读1分钟

在使用AVAudioRecorder录音保存.wav文件的时候,iOS11以下的系统,wav的头部信息是没问题的,但是iOS11+头部信息服务区识别不了。可以通过setting的设置来解决这个问题。

if (@available(iOS 11.0, *)) {
            _recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
                              [NSNumber numberWithFloat: 24000.0],AVSampleRateKey, //采样率
                              [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                              [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数 默认 16
                              [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//通道的数目
                              @(kAudioFileWAVEType),AVAudioFileTypeKey,
                              nil];
        } else {
            // Fallback on earlier versions
            _recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
            [NSNumber numberWithFloat: 24000.0],AVSampleRateKey, //采样率
            [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
            [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数 默认 16
            [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//通道的数目
            nil];
        }

iOS11要设置AVAudioFileTypeKey。