美文网首页
ios之录音

ios之录音

作者: 海里的神秘骑士 | 来源:发表于2017-02-19 11:52 被阅读0次

    代码:

    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @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之录音

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