這篇文章主要為大家展示了“iOS10和Xcode8如何創(chuàng)建Siri功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“iOS10和Xcode8如何創(chuàng)建Siri功能”這篇文章吧。
創(chuàng)新互聯(lián)公司溝通電話:13518219792,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)頁(yè)制作領(lǐng)域10余年,包括OPP膠袋等多個(gè)方面擁有多年的營(yíng)銷(xiāo)推廣經(jīng)驗(yàn),選擇創(chuàng)新互聯(lián)公司,為企業(yè)錦上添花!
iOS 10 出來(lái)之后,我們開(kāi)發(fā)者也可以使用類(lèi)似Siri的功能。。讓我們來(lái)看看怎么使用吧,其實(shí)他是使用Siri里面的一個(gè)語(yǔ)音識(shí)別框架Speech framework。 讓我們來(lái)看看 一些 主要的代碼吧。 我們需要一個(gè) UITextView 和 UIButton 就 能體現(xiàn)了。
第一步:定義屬性
@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
第二步:進(jìn)行語(yǔ)音識(shí)別檢測(cè)
- (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(@"可以語(yǔ)音識(shí)別"); break; case SFSpeechRecognizerAuthorizationStatusDenied: isButtonEnabled = false; NSLog(@"用戶(hù)被拒絕訪問(wèn)語(yǔ)音識(shí)別"); break; case SFSpeechRecognizerAuthorizationStatusRestricted: isButtonEnabled = false; NSLog(@"不能在該設(shè)備上進(jìn)行語(yǔ)音識(shí)別"); break; case SFSpeechRecognizerAuthorizationStatusNotDetermined: isButtonEnabled = false; NSLog(@"沒(méi)有授權(quán)語(yǔ)音識(shí)別"); break; default: break; } self.siriBtu.enabled = isButtonEnabled; }]; self.audioEngine = [[AVAudioEngine alloc]init]; }
第三步:按鈕的點(diǎn)擊事件
- (void)microphoneTap:(UIButton *)sender { if ([self.audioEngine isRunning]) { [self.audioEngine stop]; [self.recognitionRequest endAudio]; self.siriBtu.enabled = YES; [self.siriBtu setTitle:@"開(kāi)始錄制" forState:UIControlStateNormal]; }else{ [self startRecording]; [self.siriBtu setTitle:@"停止錄制" forState:UIControlStateNormal]; }}
第四步 :開(kāi)始錄制語(yǔ)音,以及將語(yǔ)音轉(zhuǎn)為文字
-(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(@"這里說(shuō)明有的功能不支持"); } 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 冰,你說(shuō)我聽(tīng)"; }
最后一個(gè)代理方法:
-(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{ if(available){ self.siriBtu.enabled = true; }else{ self.siriBtu.enabled = false; } }
這樣我們 就可以實(shí)現(xiàn)Siri的功能了。
以上是“iOS10和Xcode8如何創(chuàng)建Siri功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:iOS10和Xcode8如何創(chuàng)建Siri功能
URL網(wǎng)址:http://www.rwnh.cn/article28/jscijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站策劃、商城網(wǎng)站、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)