原理:先用系統的錄音器錄音,讓後讓siri識別語音轉文字app
第一步 :在項目plist文件添加受權如圖ide
第二步:導入頭文件,添加協議
#import <Speech/Speech.h>
#import <AVFoundation/AVFoundation.h>atom
第三步:UI控件,實現siri識別方法,代碼以下:
--------------------- .net
@interface ViewController ()<SFSpeechRecognizerDelegate> @property (strong, nonatomic) UIButton *siriBtu;//siri按鈕 @property (strong, nonatomic) UITextView *siriTextView; //顯示語音轉化成的文本 @property (strong, nonatomic) SFSpeechRecognitionTask *recognitionTask; //語音識別任務 @property (strong, nonatomic)SFSpeechRecognizer *speechRecognizer; //語音識別器 @property (strong, nonatomic) SFSpeechAudioBufferRecognitionRequest *recognitionRequest; //識別請求 @property (strong, nonatomic)AVAudioEngine *audioEngine; //錄音引擎 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.siriBtu = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,300,200)]; self.siriBtu.backgroundColor = [UIColor yellowColor]; [self.siriBtu addTarget:self action:@selector(microphoneTap:) forControlEvents:UIControlEventTouchUpInside]; self.siriBtu.titleLabel.textColor = [UIColor redColor]; [self.view addSubview:_siriBtu]; self.siriTextView = [[UITextView alloc]initWithFrame:CGRectMake(0,500,100,200)]; [self.view addSubview:_siriTextView]; //設備識別語言爲中文 NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"]; self.speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:cale]; self.siriBtu.enabled = false; //設置代理 _speechRecognizer.delegate = self; //發送語音認證請求(首先要判斷設備是否支持語音識別功能) [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) { bool isButtonEnabled = false; switch (status) { case SFSpeechRecognizerAuthorizationStatusAuthorized: isButtonEnabled = true; NSLog(@"能夠語音識別"); break; case SFSpeechRecognizerAuthorizationStatusDenied: isButtonEnabled = false; NSLog(@"用戶被拒絕訪問語音識別"); break; case SFSpeechRecognizerAuthorizationStatusRestricted: isButtonEnabled = false; NSLog(@"不能在該設備上進行語音識別"); break; case SFSpeechRecognizerAuthorizationStatusNotDetermined: isButtonEnabled = false; NSLog(@"沒有受權語音識別"); break; default: break; } self.siriBtu.enabled = isButtonEnabled; }]; //建立錄音引擎 self.audioEngine = [[AVAudioEngine alloc]init]; } - (void)microphoneTap:(UIButton *)sender { if ([self.audioEngine isRunning]) { [self.audioEngine stop]; [self.recognitionRequest endAudio]; self.siriBtu.enabled = YES; [self.siriBtu setTitle:@"開始錄製" forState:UIControlStateNormal]; }else{ [self startRecording]; [self.siriBtu setTitle:@"中止錄製" forState:UIControlStateNormal]; } } -(void)startRecording{ if (self.recognitionTask) { [self.recognitionTask cancel]; self.recognitionTask = nil; } AVAudioSession *audioSession = [AVAudioSession sharedInstance]; bool audioBool = [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; bool audioBool1= [audioSession setMode:AVAudioSessionModeMeasurement error:nil]; bool audioBool2= [audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; if (audioBool || audioBool1|| audioBool2) { NSLog(@"可使用"); }else{ NSLog(@"這裏說明有的功能不支持"); } self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init]; AVAudioInputNode *inputNode = self.audioEngine.inputNode; SFSpeechAudioBufferRecognitionRequest *recognitionRequest; self.recognitionRequest.shouldReportPartialResults = true; //開始識別任務 self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) { bool isFinal = false; if (result) { self.siriTextView.text = [[result bestTranscription] formattedString]; //語音轉文本 NSLog(@"打印識別的內容%@",[[result bestTranscription] formattedString]); isFinal = [result isFinal]; } if (error || isFinal) { [self.audioEngine stop]; [inputNode removeTapOnBus:0]; self.recognitionRequest = nil; self.recognitionTask = nil; self.siriBtu.enabled = true; } }]; AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0]; [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) { [self.recognitionRequest appendAudioPCMBuffer:buffer]; }]; [self.audioEngine prepare]; bool audioEngineBool = [self.audioEngine startAndReturnError:nil]; NSLog(@"%d",audioEngineBool); self.siriTextView.text = @"我是小冰!Siri 冰,你說我聽"; } -(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{ if(available){ self.siriBtu.enabled = true; }else{ self.siriBtu.enabled = false; } } --------------------- 做者:Z苗 來源:CSDN 原文:https://blog.csdn.net/Z1591090/article/details/82787716 版權聲明:本文爲博主原創文章,轉載請附上博文連接!
日語:ja_JP代理