需要调用到麦克风方法,别忘记添加 Privacy - Microphone Usage Description
文章来源:https://www.toymoban.com/news/detail-609119.html
@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
伊织 2020-02-28文章来源地址https://www.toymoban.com/news/detail-609119.html
到了这里,关于AVFoundation - 音频录制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!