录音

#import "RecordViewController.h" #import #define kRecordAudioFile @"myRecord.caf"@interface RecordViewController () @property (nonatomic,strong) AVAudioRecorder *audioRecorder; //音频录音机 @property (nonatomic,strong) AVAudioPlayer *audioPlayer; //音频播放器,用于播放录音文件 @property (nonatomic,strong) NSTimer *timer; //录音声波监控(注意这里暂时不对播放进行监控)@property (strong, nonatomic) UIButton *record; //开始录音 @property (strong, nonatomic) UIButton *pause; //暂停录音 @property (strong, nonatomic) UIButton *resume; //恢复录音 @property (strong, nonatomic) UIButton *stop; //停止录音 @property (strong, nonatomic) UIProgressView *audioPower; //音频波动 @end@implementation RecordViewController- (void)viewDidLoad { [super viewDidLoad]; [self createSub]; [self setAudioSession]; }- (void)createSub { //录音 self.record = [UIButton buttonWithType:UIButtonTypeSystem]; self.record.frame = CGRectMake(10, 500, 60, 40); self.record.backgroundColor = [UIColor orangeColor]; [self.record setTitle:@"录音" forState:UIControlStateNormal]; [self.record addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.record]; //暂停 self.pause = [UIButton buttonWithType:UIButtonTypeSystem]; self.pause.frame = CGRectMake(80, 500, 60, 40); self.pause.backgroundColor = [UIColor orangeColor]; [self.pause setTitle:@"暂停" forState:UIControlStateNormal]; [self.pause addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.pause]; //回复 self.resume = [UIButton buttonWithType:UIButtonTypeSystem]; self.resume.frame = CGRectMake(150, 500, 60, 40); self.resume.backgroundColor = [UIColor orangeColor]; [self.resume setTitle:@"恢复" forState:UIControlStateNormal]; [self.resume addTarget:self action:@selector(resume:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.resume]; //停止 self.stop = [UIButton buttonWithType:UIButtonTypeSystem]; self.stop.frame = CGRectMake(220, 500, 60, 40); self.stop.backgroundColor = [UIColor orangeColor]; [self.stop setTitle:@"停止" forState:UIControlStateNormal]; [self.stop addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.stop]; self.audioPower = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; self.audioPower.frame = CGRectMake(20, 200, 100, 5); self.audioPower.progressTintColor = [UIColor orangeColor]; self.audioPower.trackTintColor = [UIColor blueColor]; [self.view addSubview:self.audioPower]; }#pragma mark - 私有方法 /** *设置音频会话 */ -(void)setAudioSession{ AVAudioSession *audioSession=[AVAudioSession sharedInstance]; //设置为播放和录音状态,以便可以在录制完之后播放录音 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; }/** *取得录音文件保存路径 * *@return 录音文件路径 */ -(NSURL *)getSavePath{ NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; urlStr=[urlStr stringByAppendingPathComponent:kRecordAudioFile]; NSLog(@"file path:%@",urlStr); NSURL *url=[NSURL fileURLWithPath:urlStr]; return url; }/** *取得录音文件设置 * *@return 录音设置 */ -(NSDictionary *)getAudioSetting{ NSMutableDictionary *dicM=[NSMutableDictionary dictionary]; //设置录音格式 [dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey]; //设置录音采样率,8000是电话采样率,对于一般录音已经够了 [dicM setObject:@(8000) forKey:AVSampleRateKey]; //设置通道,这里采用单声道 [dicM setObject:@(1) forKey:AVNumberOfChannelsKey]; //每个采样点位数,分为8、16、24、32 [dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey]; //是否使用浮点数采样 [dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey]; //....其他设置等 return dicM; }/** *获得录音机对象 * *@return 录音机对象 */ -(AVAudioRecorder *)audioRecorder{ if (!_audioRecorder) { //创建录音文件保存路径 NSURL *url = [self getSavePath]; //创建录音格式设置 NSDictionary *setting = [self getAudioSetting]; //创建录音机 NSError *error = nil; _audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error]; _audioRecorder.delegate = self; _audioRecorder.meteringEnabled=YES; //如果要监控声波则必须设置为YES if (error) { NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription); return nil; } } return _audioRecorder; }/** *创建播放器 * *@return 播放器 */ -(AVAudioPlayer *)audioPlayer{ if (!_audioPlayer) { NSURL *url = [self getSavePath]; NSError *error=nil; _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; _audioPlayer.numberOfLoops=0; [_audioPlayer prepareToPlay]; if (error) { NSLog(@"创建播放器过程中发生错误,错误信息:%@",error.localizedDescription); return nil; } } return _audioPlayer; }/** *录音声波监控定制器 * *@return 定时器 */ -(NSTimer *)timer{ if (!_timer) { _timer=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(audioPowerChange) userInfo:nil repeats:YES]; } return _timer; }/** *录音声波状态设置 */ -(void)audioPowerChange{ [self.audioRecorder updateMeters]; //更新测量值 float lowPassResults = pow(10, (0.05 * [self.audioRecorder peakPowerForChannel:0])); NSLog(@"%.2f", lowPassResults); CGFloat pro = 0; if (0

    推荐阅读