ios之录音

代码:

#import "ViewController.h"
#import 

@interface ViewController ()

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



- (IBAction)startBtn {
    
    
    // 6.开始录音
    [self.recorder record];
    
}

- (IBAction)stopBtn {
    // 结束录音
    [self.recorder stop];
}

#pragma mark - 懒加载代码
- (AVAudioRecorder *)recorder{
    if (_recorder == nil) {
        // 1.创建沙盒路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSLog(@"%@",path);
        
        // 2.拼接音频文件
        NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"];
        
        // 3.转化成URL
        NSURL *url = [NSURL fileURLWithPath:filePath];
        
        // 4.设置录音的参数
        NSDictionary *settingRecorder = @{
                                          AVEncoderAudioQualityKey : [NSNumber numberWithInteger:AVAudioQualityLow],
                                          AVEncoderBitRateKey : [NSNumber numberWithInteger:16],
                                          AVSampleRateKey : [NSNumber numberWithFloat:8000],
                                          AVNumberOfChannelsKey : [NSNumber numberWithInteger:2]
                                          };
        
        // 5.创建录音对象
        self.recorder = [[AVAudioRecorder alloc]initWithURL:url settings:settingRecorder error:nil];
    }
    return _recorder;
}

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