我尝试实现录音和录像功能。对于音频,请使用 AVAudioRecorder ,对于视频,请使用AVFoundation(AVCaptureSession + AVAssetWriter)。
这是我的音频设置
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
[session setActive:YES error:&error];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMedium] forKey:AVEncoderAudioQualityKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
self.recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:&error];
self.recorder.delegate = self;
self.recorder.meteringEnabled = NO;
[self.recorder prepareToRecord];
这是我的catpure会话设置
NSError *error = nil;
_captureSession = [[AVCaptureSession alloc] init];
_captureSession.usesApplicationAudioSession = NO;
_captureDeviceAudio = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
_captureDeviceInputAudio = [AVCaptureDeviceInput deviceInputWithDevice:_captureDeviceAudio error:&error];
// capture device ouputs
_captureOutputAudio = [[AVCaptureAudioDataOutput alloc] init];
_captureOutputVideo = [[AVCaptureVideoDataOutput alloc] init];
[_captureOutputAudio setSampleBufferDelegate:self queue:_captureCaptureDispatchQueue];
[_captureOutputVideo setSampleBufferDelegate:self queue:_captureCaptureDispatchQueue];
// Skip code here add AVCaptureDeviceInput and audio/video input/output
通过以下方式解决问题。
这对我来说很奇怪。通过设置usesAppicationAudioSession = NO,捕获会话将维护一个私有音频会话,其设置与共享的audioSession分开。如果是这种情况,我在共享的AudioSession(在我的音频设置中)更改的任何内容不应该影响