zoukankan      html  css  js  c++  java
  • IOS开发之 一个实例解析 录音

    导入框架:

    #import <AVFoundation/AVFoundation.h>

    声明全局变量:

    @interface ViewController ()<AVAudioRecorderDelegate>

    {

        AVAudioRecorder *audioRecorder;

    }

    @end

     

    在ViewDidLoad中:
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 100, 100);
        [button setTitle:@"TICK" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor brownColor];
        [button addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    按钮的触发事件
    - (void)startAudioRecoder:(UIButton *)sender{
        sender.selected = !sender.selected;
        if (sender.selected != YES) {
            [audioRecorder stop];
            return;
        }
        
        //    URL是本地的URL  AVAudioRecorder需要一个存储的路径
        NSString *name = [NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970];
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];
        NSError *error;
        //    录音机  初始化
        audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];
        [audioRecorder prepareToRecord];
        [audioRecorder record];
        audioRecorder.delegate = self;
        /*
         1.AVNumberOfChannelsKey 通道数 通常为双声道 值2
         2.AVSampleRateKey 采样率  单位HZ 通常设置成44100 也就是44.1k
         3.AVLinearPCMBitDepthKey 比特率 8 16 24 32
         4.AVEncoderAudioQualityKey 声音质量
                 ① AVAudioQualityMin    = 0, 最小的质量
                 ② AVAudioQualityLow    = 0x20, 比较低的质量
                 ③ AVAudioQualityMedium = 0x40, 中间的质量
                 ④ AVAudioQualityHigh   = 0x60,高的质量
                 ⑤ AVAudioQualityMax    = 0x7F 最好的质量
         5.AVEncoderBitRateKey 音频编码的比特率 单位Kbps 传输的速率  一般设置128000 也就是128kbps
         
         */
        
        
        
        NSLog(@"%@",path);
    
    }
    
    
    
    代理方法:
    - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
        NSLog(@"录音结束");
    //    文件操作的类
      NSFileManager *manger = [NSFileManager defaultManager];
    
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //    获得当前文件的所有子文件subpathsAtPath
       NSArray *pathlList = [manger subpathsAtPath:path];
    
    //    需要只获得录音文件
        NSMutableArray *audioPathList = [NSMutableArray array];
    //    遍历所有这个文件夹下的子文件
        for (NSString *audioPath in pathlList) {
    //        通过对比文件的延展名(扩展名 尾缀)  来区分是不是录音文件
            if ([audioPath.pathExtension isEqualToString:@"aiff"]) {
    //            把筛选出来的文件放到数组中
                [audioPathList addObject:audioPath];
            }
        }
        
        NSLog(@"%@",audioPathList);
        
    }
  • 相关阅读:
    sql基本语法:
    mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': You have an error in your SQL syntax; check the manual t
    truncate和 delete的区别:
    主流存储引擎详解:Innodb,Tokudb、Memory、MYISAM、Federated
    ant-design-vue表单生成组件form-create快速上手
    vue自定义表单生成器,可根据json参数动态生成表单
    Vue数据驱动表单渲染,轻松搞定form表单
    PHP表单生成器,快速生成现代化的form表单,快速上手
    form-create 组件生成规则说明
    form-create教程:自定义布局,实现一行多个组件
  • 原文地址:https://www.cnblogs.com/ios-wanglong/p/5282064.html
Copyright © 2011-2022 走看看