ios wav格式語音文件轉(zhuǎn)文字

一、錄音設(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];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容