iOS_37_录音

来源:互联网 发布:用哪个软件刷机好 编辑:程序博客网 时间:2024/04/29 04:51




录音机参数


强耦合的控制器
////  BeyondViewController.m//  37_录音////  Created by beyond on 14-9-16.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"// 录音必须导入本框架#import <AVFoundation/AVFoundation.h>@interface BeyondViewController ()#pragma mark - 成员属性// 录音机@property (nonatomic, strong) AVAudioRecorder *recorder;// 时钟(监听???)@property (nonatomic, strong) CADisplayLink *link;// flag用于标记 无声的时间长度,用于判断停止录音条件@property (nonatomic, assign) double slientDuration;#pragma mark - 界面连线// 开始录音- (IBAction)startRecord;// 停止录音- (IBAction)stopRecord;// 播放录音(使用音乐工具类SongTool)- (IBAction)startPlay;@end@implementation BeyondViewController#pragma mark - 懒加载- (CADisplayLink *)link{    if (!_link) {        self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];    }    return _link;}#pragma mark - 连线方法// 开始录音- (IBAction)startRecord{        // 1.创建录音机,必须指定文件保存的fileURL,和录音的初始化参数    [self setupAudioRecord];        // 4.开启定时器    self.slientDuration = 0;    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];}// 核心代码~ ~ ~创建录音机,必须指定文件保存的fileURL,和录音的初始化参数- (void)setupAudioRecord{    // 1.指定录音文件的保存的FileURL    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"test.caf"];    NSURL *url = [NSURL fileURLWithPath:path];        // 2.设置录音机的取样参数(字典)    NSMutableDictionary *setting = [NSMutableDictionary dictionary];    // 音频格式    setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);    // 音频采样率    setting[AVSampleRateKey] = @(8000.0);    // 音频通道数    setting[AVNumberOfChannelsKey] = @(1);    // 线性音频的位深度    setting[AVLinearPCMBitDepthKey] = @(8);        // 3.根据上两个条件参数,可以创建录音机    AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setting error:nil];        // 4.设置录音机的属性,比如允许测量分贝(必须设置)    recorder.meteringEnabled = YES;        // 5.录音机预备缓冲    [recorder prepareToRecord];        // 6.录音机 录音    [recorder record];        self.recorder = recorder;}// 停止录音- (IBAction)stopRecord{    [self.recorder stop];}// 播放录音- (IBAction)startPlay{    }#pragma mark - 时钟方法// 自动根据 无声时长,停止录音- (void)update{    // 1.必需先更新录音机的测量值,才可以获得分贝值    [self.recorder updateMeters];        // 2.才能够 获得平均分贝值(安静时 -50左右,大声说话是-10左右)    float power = [self.recorder averagePowerForChannel:0];        // 3.如果小于-30, 开始静音    if (power < - 30) {        // 标记,累记无意时长        self.slientDuration += self.link.duration;        // 如果 沉默至少2秒钟,预示着说话结束        if (self.slientDuration >= 2) {            // 停止录音            [self.recorder stop];            // 停止定时器,并置空            [self.link invalidate];            self.link = nil;            NSLog(@"--------停止录音");        }    } else {        // 说明正在大声喧哗,清零标记        self.slientDuration = 0;        NSLog(@"**********正在大声喧哗");    }}@end



录音工具类
////  RecordTool.h//  37_录音////  Created by beyond on 14-9-16.//  Copyright (c) 2014年 com.beyond. All rights reserved.//  录音工具类#import <Foundation/Foundation.h>// 录音必须导入本框架#import <AVFoundation/AVFoundation.h>@interface RecordTool : NSObject// 通过类方法,快速返回一个录音机对象,参数是:目标文件保存名+(AVAudioRecorder *) record:(NSString *)destFileName;@end



////  RecordTool.m//  37_录音////  Created by beyond on 14-9-16.//  Copyright (c) 2014年 com.beyond. All rights reserved.//  录音工具类#import "RecordTool.h"@implementation RecordTool// 通过类方法,快速返回一个录音机对象,参数是:目标文件保存名+(AVAudioRecorder *)record:(NSString *)destFileName{        // 核心代码~ ~ ~创建录音机,必须指定文件保存的fileURL,和录音的初始化参数    // 1.指定录音文件的保存的FileURL    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:destFileName];    NSURL *url = [NSURL fileURLWithPath:path];        // 2.设置录音机的取样参数(字典)    NSMutableDictionary *setting = [NSMutableDictionary dictionary];    // 音频格式    setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);    // 音频采样率    setting[AVSampleRateKey] = @(8000.0);    // 音频通道数    setting[AVNumberOfChannelsKey] = @(1);    // 线性音频的位深度    setting[AVLinearPCMBitDepthKey] = @(8);        // 3.根据上两个条件参数,可以创建录音机    AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setting error:nil];        // 4.设置录音机的属性,比如允许测量分贝(必须设置)    recorder.meteringEnabled = YES;        // 5.录音机预备缓冲    [recorder prepareToRecord];        // 6.录音机 开始录音    // [recorder record];            return recorder;    }@end






1 0
原创粉丝点击