#pragma mark - 设置录制的音频文件的位置 - (NSString *)audioRecordingPath{ NSString *str_date=[TimeTransform DateToStringWithFormat:[NSDate date] withFormatString:@"yyyyMMDDHHmmss"]; AvdioName=str_date; NSString *result = nil; NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsFolde = [folders objectAtIndex:0]; result = [documentsFolde stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.aac",str_date]]; return (result); } #pragma mark -根据文件名获取文件路径 -(NSString *)getRecordingPathWithName:(NSString *)name{ NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsFolde = [folders objectAtIndex:0]; NSString *result = nil; result = [documentsFolde stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.aac",name]]; return result; } #pragma mark -录音按钮 -(void)SoundAction:(UILongPressGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer.state==UIGestureRecognizerStateBegan) { NSLog(@"UIGestureRecognizerStateBegan"); //按下录音 if ([self canRecord]) { NSError *error = nil; //必须真机上测试,模拟器上可能会崩溃 recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:[self audioRecordingPath]] settings:recorderSettingsDict error:&error]; if (recorder) { recorder.meteringEnabled = YES; [recorder prepareToRecord]; [recorder record]; //启动定时器 timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES]; NSLog(@"录制声音开始!"); [self performSelector:@selector(stopRecordingOnAudioRecorder:) withObject:recorder afterDelay:10.0f]; [view_black_bg setHidden:false]; } else { int errorCode = CFSwapInt32HostToBig ([error code]); NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); } } }else if (gestureRecognizer.state==UIGestureRecognizerStateEnded){ NSLog(@"UIGestureRecognizerStateEnded"); [lb_audio_title setText:AvdioName]; [self stopRecordingOnAudioRecorder:recorder]; [timer invalidate]; [view_black_bg setHidden:YES]; } } -(void)levelTimer:(NSTimer*)timer_ { //call to refresh meter values刷新平均和峰值功率,此计数是以对数刻度计量的,-160表示完全安静,0表示最大输入值 [recorder updateMeters]; const double ALPHAa = 0.05; double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0])); lowPassResults = ALPHAa * peakPowerForChannel + (1.0 - ALPHAa) * lowPassResults; NSLog(@"Average input: %f Peak input: %f Low pass results: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0], lowPassResults); // if (lowPassResults>=0.8) { // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:7]]; // }else if(lowPassResults>=0.7){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:6]]; // }else if(lowPassResults>=0.6){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:5]]; // }else if(lowPassResults>=0.5){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:4]]; // }else if(lowPassResults>=0.4){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:3]]; // }else if(lowPassResults>=0.3){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:2]]; // }else if(lowPassResults>=0.2){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:1]]; // }else if(lowPassResults>=0.1){ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]]; // }else{ // soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]]; // } } //判断是否允许使用麦克风7.0新增的方法requestRecordPermission -(BOOL)canRecord { __block BOOL bCanRecord = YES; if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending) { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) { [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) { if (granted) { bCanRecord = YES; } else { bCanRecord = NO; dispatch_async(dispatch_get_main_queue(), ^{ [[[UIAlertView alloc] initWithTitle:nil message:@"app需要访问您的麦克风。 请启用麦克风-设置/隐私/麦克风" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil] show]; }); } }]; } } return bCanRecord; } #pragma mark -停止音频的录制 - (void)stopRecordingOnAudioRecorder:(AVAudioRecorder *)record{ [recorder stop]; recorder=nil; //结束定时器 [timer invalidate]; timer = nil; } /** * 录音完成,录音完成后播放录音 * * @param recorder 录音机对象 * @param flag 是否成功 */ -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ }