使用AVAudioRecorder录制音频时实时音高变化

时间:2018-04-18 08:15:15

标签: ios objective-c

我正在尝试实现一种功能,我可以录制视频并像实时外星人一样在实时中应用效果。因此,当我演奏它时,听起来就像是外星人。

我已经实现了在录制音频后我可以改变音频的音高,但现在想在录制音频时这样做。

以下是录制音频及其设置的代码。

NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary
                                dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0],
                                AVSampleRateKey,
                                nil];
NSError *error = nil;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

_audioRecorder = [[AVAudioRecorder alloc]
                  initWithURL:soundFileURL
                  settings:recordSettings
                  error:&error];

_audioRecorder.delegate = self;
_audioRecorder.meteringEnabled = YES;


if (error) {
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [_audioRecorder prepareToRecord];
}

1 个答案:

答案 0 :(得分:8)

使用AVCaptureAudioDataOutput代替AVAudioRecorder,它可以让您在录制时处理音频数据:

_captureAudioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
[_captureAudioDataOutput setAudioSettings:recordSettings];
[_captureAudioDataOutput setSampleBufferDelegate:self queue:_dispatchQueue];

您需要AVCaptureSessionself必须提供captureOutput功能。

captureOutput中,您可以使用已有的代码更改音频音调。

相关问题