AVF 3 - 音频录制

守給你的承諾、 提交于 2020-02-28 17:55:42

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!