一、錄音設(shè)置
? ??NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
? ? //設(shè)置錄音格式? AVFormatIDKey==kAudioFormatLinearPCM
? ? [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
? ? //設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
? ? [recordSetting setValue:[NSNumber numberWithFloat:16000] forKey:AVSampleRateKey];
? ? //錄音通道數(shù)? 1 或 2
? ? [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
? ? //線性采樣位數(shù)? 8、16、24、32
? ? [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
? ? //錄音的質(zhì)量
? ? [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
? ? NSString *strUrl = [NSString stringWithFormat:@"%@/%@.wav", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], [NSString uuid]];
? ? NSURL *url = [NSURL fileURLWithPath:strUrl];
? ? self.recordUrlKey = strUrl;
//? ? self.audioRecord.filePath = strUrl;
? ? NSError *error;
? ? //初始化
? ? _recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];
? ? //開啟音量檢測
? ? self.recorder.meteringEnabled = YES;
? ? if ([self.recorder prepareToRecord]){
? ? ? ? return YES;
? ? }
二、wav 轉(zhuǎn) m4a 格式
NSURL *audioPath = [NSURL fileURLWithPath:path];
? ? AVURLAsset* audioAsset = [[AVURLAsset alloc] initWithURL:audioPath options:nil];
? ? AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:audioAsset presetName:AVAssetExportPresetAppleM4A];
? ? NSURL* exportURL = [NSURL fileURLWithPath:[[path componentsSeparatedByString:@".wav"] objectAtIndex:0]];
? ? NSURL* destinationURL = [exportURL URLByAppendingPathExtension:@"m4a"];
? ? exportSession.outputURL = destinationURL;
? ? exportSession.outputFileType = AVFileTypeAppleM4A;
? ? [exportSession exportAsynchronouslyWithCompletionHandler:^{
? ? ? ? if (AVAssetExportSessionStatusCompleted == exportSession.status) {
? ? ? ? } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
? ? ? ? } else {
? ? ? ? }
? ? ? ? didFinish();
? ? }];
三、m4a 翻譯
NSURL *exportURL = [NSURL fileURLWithPath:[[path componentsSeparatedByString:@".wav"] objectAtIndex:0]];
? ? ? ? ? ? NSURL *destinationURL = [exportURL URLByAppendingPathExtension:@"m4a"];
? ? ? ? ? ? NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
? ? ? ? ? ? SFSpeechRecognizer *localRecognizer =[[SFSpeechRecognizer alloc] initWithLocale:local];
? ? ? ? ? ? NSURL *url = destinationURL;
? ? ? ? ? ? if (!url) { [AlertUtil showAlertWithText:@"轉(zhuǎn)換失敗"]; return;}
? ? ? ? ? ? SFSpeechURLRecognitionRequest *res =[[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
? ? ? ? ? ? __weak typeof(self) weakSelf = self;
? ? ? ? ? ? [localRecognizer recognitionTaskWithRequest:res resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
? ? ? ? ? ? ? ? if (error) {
? ? ? ? ? ? ? ? ? ? NSLog(@"------------------------語音識別解析======失敗,%@",error);
? ? ? ? ? ? ? ? ? ? [AlertUtil showAlertWithText:@"轉(zhuǎn)換失敗"];
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? NSLog(@"------------------------語音識別解析======成功,%@",result.bestTranscription.formattedString);
? ? ? ? ? ? ? ? ? ? [weakSelf showWord:result.bestTranscription.formattedString];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }];