您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“iOS10和Xcode8如何創建Siri功能”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“iOS10和Xcode8如何創建Siri功能”這篇文章吧。
iOS 10 出來之后,我們開發者也可以使用類似Siri的功能。。讓我們來看看怎么使用吧,其實他是使用Siri里面的一個語音識別框架Speech framework。 讓我們來看看 一些 主要的代碼吧。 我們需要一個 UITextView 和 UIButton 就 能體現了。
第一步:定義屬性
@interface ViewController () <SFSpeechRecognizerDelegate> @property (strong, nonatomic) UIButton *siriBtu; @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
第二步:進行語音識別檢測
- (void)viewDidLoad { [super viewDidLoad]; 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]; 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; } }
這樣我們 就可以實現Siri的功能了。
以上是“iOS10和Xcode8如何創建Siri功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。