因为开发一直在模拟器上测试,一直没注意到这个问题,第一反应还以为权限问题,查看了之后权限没毛病。上代码看问题。
-(AVAudioRecorder *)record {
if (!_record) {
self.path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pcm",[self getCurrentTimes]]];
NSDictionary *configDic = @{// 编码格式
AVFormatIDKey:@(kAudioFormatLinearPCM),
// 采样率
AVSampleRateKey:@(11025.0),
// 通道数
AVNumberOfChannelsKey:@(2),
// 录音质量
AVEncoderAudioQualityKey:@(AVAudioQualityMin)
};
NSError *error = nil;
_record = [[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString: self.path ] settings:configDic error:&error];
_record.meteringEnabled = YES;
if (error) {
NSLog(@"error:%@",error);
}
// 准备录音(系统会给我们分配一些资源)
[_record prepareToRecord];
}
return _record;
}
然后就是查找问题,解决问题。
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
}
在代码中加上这段代码就能录音了。