美文网首页
iOS开发-基础录音

iOS开发-基础录音

作者: Flynn_Lee | 来源:发表于2020-12-27 12:40 被阅读0次

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;;
}

相关文章

网友评论

      本文标题:iOS开发-基础录音

      本文链接:https://www.haomeiwen.com/subject/xnronktx.html