iOS 10 和Xcode8 一起 创建 Siri 功能步骤详解(OC写的 )
程序员文章站
2023-12-19 22:11:58
ios 10 出来之后,我们开发者也可以使用类似siri的功能。。让我们来看看怎么使用吧,其实他是使用siri里面的一个语音识别框架speech framework。 让我...
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的功能了。
总结
以上所述是小编给大家介绍的ios 10 和xcode8 一起 创建 siri 功能步骤详解(oc写的 ),希望对大家有所帮助