iOS开发-基础录音

2020-12-27  本文已影响0人  Flynn_Lee

1、导入对应头文件

#import <AVFoundation/AVFoundation.h>

2、在storyboard上布局两个按钮,开始录音和停止录音

image.png

3、连接两个按钮的点击事件

- (IBAction)start:(id)sender {
    
    [self.recorder record];
}
- (IBAction)stop:(id)sender {
    
    [self.recorder stop];
}

4、写一个全局的recorder

/**
 录音全局变量
 */
@property(nonatomic,strong)AVAudioRecorder *recorder;

5、给全局变量写懒加载

-(AVAudioRecorder *)recorder
{
    if (_recorder == nil) {
       
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSLog(@"%@",path);
        
        NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"];
        
        NSURL *url = [NSURL fileURLWithPath:filePath];
        
        NSDictionary *dict = @{
            AVEncoderAudioQualityKey:[NSNumber numberWithInteger:AVAudioQualityLow],
            AVEncoderBitRateKey:[NSNumber numberWithInteger:16],
            AVSampleRateKey:[NSNumber numberWithFloat:8000],
            AVNumberOfChannelsKey:[NSNumber numberWithInteger:2]
        };
        
        self.recorder = [[AVAudioRecorder alloc]initWithURL:url settings:dict error:nil];
    }
    
    return _recorder;;
}
上一篇 下一篇

猜你喜欢

热点阅读