这篇文章主要为大家展示了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。