录音

1.录音

#import "ViewController.h"
#import 

@interface ViewController ()

/** 录音的对象 */
@property (nonatomic, strong) AVAudioRecorder *recoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)startRecord:(id)sender {
    
    // 4.开始录音
    [self.recoder record];
}

- (IBAction)stopRcord:(id)sender {
    [self.recoder stop];
}

#pragma mark - 懒加载代码
- (AVAudioRecorder *)recoder
{
    if (_recoder == nil) {
        // 1.创建存放录音文件的地址
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [path stringByAppendingPathComponent:@"321.caf"];
        NSURL *url = [NSURL URLWithString:filePath];
        
        // 2.创建录音对象
        self.recoder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
        
        // 3.准备录音
        [self.recoder prepareToRecord];
    }
    return _recoder;
}

@end

2.设置录制音频的质量

// 创建录音配置信息的字典
NSMutableDictionary *setting = [NSMutableDictionary dictionary];
// 音频格式
setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);
// 录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
setting[AVSampleRateKey] = @(8000.0);
// 音频通道数 1 或 2
setting[AVNumberOfChannelsKey] = @(1);
// 线性音频的位深度  8、16、24、32
setting[AVLinearPCMBitDepthKey] = @(8);
//录音的质量
setting[AVEncoderAudioQualityKey] = [NSNumber numberWithInt:AVAudioQualityHigh];

你可能感兴趣的:(录音)