美文网首页
IOS录音机功能---by talent.L

IOS录音机功能---by talent.L

作者: 天才iOS程序员 | 来源:发表于2017-02-17 11:56 被阅读80次

    一添加权限

    NSMicrophoneUsageDescription

    二导入框架

    AVFoundation.framework

    三.m中

    #import <AVFoundation/AVFoundation.h>

    @interface ViewController () <AVAudioRecorderDelegate>

    {

    AVAudioRecorder *audioRecorder;

    AVAudioPlayer *player;

    }

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];

    buttonOne.frame = CGRectMake(100, 100, 100, 100);

    [buttonOne setTitle:@"TICK" forState:UIControlStateNormal];

    buttonOne.backgroundColor = [UIColor brownColor];

    [buttonOne addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonOne];

    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];

    buttonTwo.frame = CGRectMake(100, 300, 100, 100);

    [buttonTwo setTitle:@"播放" forState:UIControlStateNormal];

    buttonTwo.backgroundColor = [UIColor redColor];

    [buttonTwo addTarget:self action:@selector(startPlayer:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonTwo];

    }

    - (void)startAudioRecoder:(UIButton *)sender{

    sender.selected = !sender.selected;

    if (sender.selected != YES) {

    [audioRecorder stop];

    return;

    }

    //  URL是本地的URL AVAudioRecorder需要一个存储的路径

    NSString *name = @"a.lyj";

    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:@"lyj"]) {

    //      把筛选出来的文件放到数组中

    [audioPathList addObject:audioPath];

    }

    }

    NSLog(@"%@",audioPathList);

    }

    -(void)startPlayer:(UIButton *)btn{

    NSString *name = @"a.lyj";

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];

    NSData *data = [[NSData data]initWithContentsOfFile:path];

    player = [[AVAudioPlayer alloc]initWithData:data error:nil];

    //设置是否可以倍速(或者放慢速度)播放

    player.enableRate = YES;

    //设置播放速度 一般0.5-2.0

    player.rate = 0.5;

    [player play];

    }

    相关文章

      网友评论

          本文标题:IOS录音机功能---by talent.L

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