AVF 3 - 音频录制
文章目录
需要调用到麦克风方法,别忘记添加 Privacy - Microphone Usage Description
@interface AudioRecorder ()<AVAudioRecorderDelegate>
@property (strong, nonatomic) AVAudioRecorder *recorder;
@end
@implementation AudioRecorder
- (void)setup{
NSString *tmpDir = NSTemporaryDirectory();
NSString *filePath = [tmpDir stringByAppendingPathComponent:@"memo.caf"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"fileURL : %@",fileURL);
NSDictionary *settings = @{
AVFormatIDKey : @(kAudioFormatAppleIMA4),
AVSampleRateKey : @44100.0f,
AVNumberOfChannelsKey : @1,
AVEncoderBitDepthHintKey : @16,
AVEncoderAudioQualityKey : @(AVAudioQualityMedium)
};
NSError *error;
self.recorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:settings error:&error];
self.recorder.delegate = self;
if (self.recorder) {
self.recorder.delegate = self;
self.recorder.meteringEnabled = YES;
[self.recorder prepareToRecord];
} else {
NSLog(@"Error: %@", [error localizedDescription]);
}
[self.recorder record];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.recorder pause];
[self save];
});
}
- (void)save{
NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate];
NSString *filename = [NSString stringWithFormat:@"%f.m4a", timestamp];
NSString *docsDir = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES).firstObject;
NSString *destPath = [docsDir stringByAppendingPathComponent:filename];
NSURL *srcURL = self.recorder.url;
NSURL *destURL = [NSURL fileURLWithPath:destPath];
NSLog(@"srcURL : %@",srcURL);
NSLog(@"destPath : %@",destPath);
NSError *error;
//如果上面 expandTilde 为YES 的时候,会移动失败
BOOL success = [[NSFileManager defaultManager] copyItemAtURL:srcURL toURL:destURL error:&error];
if (success) {
[self.recorder prepareToRecord]; // 准备下一次录制
}
#if TARGET_OS_MAC
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace selectFile:destPath inFileViewerRootedAtPath:nil];
#endif
}
#pragma mark - PM
- (BOOL)record {
return [self.recorder record];
}
- (void)pause {
[self.recorder pause];
}
#pragma mark - AVAudioRecorderDelegate
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"-- audioRecorderDidFinishRecording successfully : %d",flag);
}
/* if an error occurs while encoding it will be reported to the delegate. */
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * __nullable)error{
NSLog(@"-- audioRecorderEncodeErrorDidOccur error : %@",error);
}
#if TARGET_OS_IPHONE
/* AVAudioRecorder INTERRUPTION NOTIFICATIONS ARE DEPRECATED - Use AVAudioSession instead. */
/* audioRecorderBeginInterruption: is called when the audio session has been interrupted while the recorder was recording. The recorded file will be closed. */
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
NSLog(@"-- audioRecorderBeginInterruption ");
}
/* audioRecorderEndInterruption:withOptions: is called when the audio session interruption has ended and this recorder had been interrupted while recording. */
/* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags{
NSLog(@"-- audioRecorderEndInterruption ");
}
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withFlags:(NSUInteger)flags{
NSLog(@"-- audioRecorderEndInterruption ");
}
/* audioRecorderEndInterruption: is called when the preferred method, audioRecorderEndInterruption:withFlags:, is not implemented. */
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder{
NSLog(@"-- audioRecorderEndInterruption ");
}
#endif
来源:CSDN
作者:devwalks
链接:https://blog.csdn.net/weixin_45390999/article/details/104558713