iOS之录音

介绍

  • 录音使用的也是AVFoundation框架
  • 相关类:AVAudioRecorder
  • 可以设置的一些属性 :
    <1>AVNumberOfChannelsKey 通道数
    <2>AVSampleRateKey 采样率 一般用44100
    <3>AVLinearPCMBitDepthKey 比特率 一般设16 32
    <4>AVEncoderAudioQualityKey 质量
    <5>AVEncoderBitRateKey 比特采样率 一般是128000

使用

  • 步骤(跟音乐播放的使用是类似的)
    • 初始化
    • 开始录音
    • 停止录音

代码示例

#import "ViewController.h"
#import 

@interface ViewController ()
{

    AVAudioRecorder *audioRecorder;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor colorWithRed:0.6223 green:0.4564 blue:0.9735 alpha:1.0];
    [button setTitle:@"Play" forState:UIControlStateNormal];
    [button setTitle:@"Pause" forState:UIControlStateSelected];
    [button addTarget:self action:@selector(recorder:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [self startAudioRecorder];
}
-(void)recorder:(UIButton *)sender{
    
    sender.selected = !sender.selected;
   sender.selected != YES?[audioRecorder stop]:[audioRecorder record];
    
}
-(void)startAudioRecorder{
 
    /*
     URL:是本地的URL 是AVAudioRecorder 需要一个存储的路径
     */
    
    NSString *name =[NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970];
NSString *path = [[ NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES) firstObject] stringByAppendingPathComponent:name];
    NSError *error;
    //初始化
    /**
     AVNumberOfChannelsKey:声道数 通常为双声道 值2
     AVSampleRateKey:采样率 单位是HZ 通常设置成44100 44.1k
     AVLinearPCMBitDepthKey:比特率  8 16 32
     AVEncoderAudioQualityKey:声音质量 需要的参数是一个枚举 :
           AVAudioQualityMin    最小的质量
           AVAudioQualityLow    比较低的质量
           AVAudioQualityMedium 中间的质量
           AVAudioQualityHigh   高的质量
           AVAudioQualityMax    最好的质量
     AVEncoderBitRateKey:音频的编码比特率 BPS传输速率 一般为128000bps
     */
    audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];
    audioRecorder.delegate = self;
    [audioRecorder prepareToRecord];
    
    //这个是存储录音文件的路径可以根据这个路径查看是否录音成功
    //复制这个路径 点击桌面 commend+shift+G可以找到存放录音文件的文件夹
    NSLog(@"%@",path);

}
//获取到录音文件
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
    
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //NSFileManager文件操作的一个类
    NSFileManager *manager = [NSFileManager defaultManager];
    //获得当前文件的所有子文件:subpathsAtPath:
    NSArray *pathList = [manager subpathsAtPath:path]
    ;
    
        //需要只获得录音文件
    NSMutableArray *audioPathList = [NSMutableArray array];
        //遍历这个文件夹下面的子文件
    for (NSString *audioPath in pathList) {
        //通过对比文件的延展名(扩展名、尾缀)来区分是不是录音文件
        if ([audioPath.pathExtension isEqualToString:@"aiff"]) {
        //把筛选出来的文件放到数组中 -> 得到所有的音频文件
            [audioPathList addObject:audioPath];
        }
    }
    NSLog(@"录音结束");
    NSLog(@"%@",pathList);
    NSLog(@"%@",audioPathList);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

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