AVAudioRecorder 在真机上录制不了声音,但在模拟器上是可以录音的。
解决办法:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];
在初始化AVAudioRecorder时加一下。
然后播放的时候声音很小。
解决办法:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];
- (void)initRecorder {
//录音设置
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/test.aac", strUrl]];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];
if(setCategoryError){
NSLog(@"%@", [setCategoryError description]);
}
NSError *error;
//初始化
recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];
//开启音量检测
recorder.meteringEnabled = YES;
recorder.delegate = self;
}
- (void)startRecorder {
if (recorder == nil) {
[self initRecorder];
}
//创建录音文件,准备录音
if ([recorder prepareToRecord]) {
//开始
[recorder record];
}
}
- (void)pauseRecorder {
[recorder pause];
[timer invalidate];
}
- (void)stopRecorder {
[recorder stop];
[timer invalidate];
}