#pragma mark ===== 錄制音頻初始化
-(void)saveAudio{
? ? AVAudioSession* session =? [AVAudioSession sharedInstance];
? ? [sessionsetCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
? ? [sessionsetActive:YESerror:nil];
? ? //錄音設(shè)置
? ? NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc]init];
? ? //設(shè)置錄音格式
? ? [recordSetting? setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
? ? //設(shè)置錄音采樣率(HZ)
? ? [recordSetting setValue:[NSNumber numberWithFloat:16000] forKey:AVSampleRateKey];
? ? //錄音通道數(shù)
? ? [recordSetting setValue:[NSNumber? numberWithInt:2] forKey:AVNumberOfChannelsKey];
? ? //線性采樣位數(shù)
? ? [recordSetting? setValue:[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
? ? //錄音的質(zhì)量
? ? [recordSetting? setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
? ? //獲取沙盒路徑 作為存儲錄音文件的路徑
? ? NSString * strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
、//? ? 創(chuàng)建url
? ? NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/voice.wav",strUrl]];
? ? self.urlPlay= url;
? ? NSError* error ;
? ? //初始化AVAudioRecorder
? ? self.recorder= [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];
? ? //開啟音量監(jiān)測
? ? self.recorder.meteringEnabled = YES;
? ? self.recorder.delegate=self;
? ? if(error){
? ? ? ? NSLog(@"創(chuàng)建錄音對象時發(fā)生錯誤,錯誤信息:%@",error.localizedDescription);
? ? }
}
#pragma mark ===== 開始錄制音頻
-(void)startSaveAvdio{
? ? if([self.recorder prepareToRecord]){
? ? ? ? //開始
? ? ? ? [self.recorderrecord];
? ? ? ? NSLog(@"開始錄音");
? ? ? ? self.avdioTimer=? [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatePlayTimerValue)? userInfo:nil? repeats:YES];
? ? ? ? [self.avdioTimerfire];
? ? }
}
#pragma mark ===== 結(jié)束錄制音頻
-(void)stopSaveAvdio
{
? ? //? ? 獲取當(dāng)前錄音時長
? ? floatvoiceSize =self.recorder.currentTime;
? ? NSLog(@"錄音時長 = %f",voiceSize);
//
? ? ? ? if(voiceSize <15){
? ? ? ? ? ? [self.recorderdeleteRecording];
? ? ? ? ? ? UIAlertView* altView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"時長小于15秒黎休,重新錄制"delegate:selfcancelButtonTitle:nilotherButtonTitles:nil];
? ? ? ? ? ? [altViewshow];
? ? ? ? ? ? [selfperformSelector:@selector(performDismiss:)withObject:altViewafterDelay:1.5];
? ? ? ? }elseif(voiceSize >60){
? ? ? ? ? ? [self.recorderdeleteRecording];
? ? ? ? ? ? UIAlertView * altView = [[UIAlertView alloc]initWithTitle:nil message:@"時長大于1分鐘,重新錄制" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
? ? ? ? ? ? [altViewshow];
? ? ? ? ? ? [selfperformSelector:@selector(performDismiss:)withObject:altViewafterDelay:1.5];
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? NSArray*dataArray=[selfgetAllFile];
? ? ? ? ? ? NSLog(@"======dataArray=======%@",dataArray);
? ? ? ? ? ? if(dataArray.count>0) {
? ? ? ? ? ? ? ? if(self.addBlackAvdioBlock) {
? ? ? ? ? ? ? ? ? ? self.addBlackAvdioBlock(dataArray);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? [self.navigationController popViewControllerAnimated:YES];
? ? ? ? ? ? }
? ? ? ? }
? ? //此處需要恢復(fù)設(shè)置回放標(biāo)志,否則會導(dǎo)致其它播放聲音也會變小
? ? AVAudioSession *session = [AVAudioSession sharedInstance];
? ? [sessionsetCategory:AVAudioSessionCategoryPlayback error:nil];
? ? [self.recorderstop];
? ? [self.avdioTimer invalidate];
? ? self.avdioTimer = nil ;
? ? NSLog(@"結(jié)束錄制音頻");
}