#import<Foundation/Foundation.h>#import<Speech/Speech.h>@interfaceSpeechlistener:nsobject<SFSpeechRecognitionTaskDelegate,SFSpeechRecognizerDelegate>@property (nonatomic,strong) Sfspeechrecognizer*Listener, @property (nonatomic,strong) Sfspeechrecognitiontask*Currenttask, @property (nonatomic,assign) sfspeechrecognizerauthorizationstatus authorizationstatus; @property ( Nonatomic,strong) Sfspeechaudiobufferrecognitionrequest*currentrequest; @property (nonatomic,assign) BOOL Inrecognizer; @property (nonatomic,copy)void(^haverecognizerspeaktextblock) (NSString *text,nstimeinterval startlocation,nstimeinterval length);-(void) Startrecognizer;-(void) Givebuffer: (cmsamplebufferref) buffer;-(void) Endrecognizer;@end
#import "SpeechListener.h"@interfaceSpeechlistener () @property (nonatomic,copy) NSString*Currenttext; @property (nonatomic,assign) nstimeinterval beginlistenlocation;@end@implementationSpeechlistener-(instancetype) init{ Self=[Super Init]; if(self) {Self.listener= [[Sfspeechrecognizer alloc] Initwithlocale:[nslocale localewithlocaleidentifier:@"ZH-CN"]]; [Self.listener setdelegate:self]; [Self requestauthorizationstatus]; } returnSelf ;}-(void) requestauthorizationstatus{typeof(self) __weak weakself =Self ; [Sfspeechrecognizer requestauthorization:^(sfspeechrecognizerauthorizationstatus status) {typeof(weakself) __strong strongself =weakself; Strongself.authorizationstatus=status; }];}-(void) startrecognizer{self.beginlistenlocation=cfabsolutetimegetcurrent (); Self.currentrequest=[[Sfspeechaudiobufferrecognitionrequest alloc] init]; [Self.currentrequest Setshouldreportpartialresults:yes]; typeof(self) __weak weakself =Self ; Self.currenttask= [Self.listener recognitionTaskWithRequest:self.currentRequest resulthandler:^ (Sfspeechrecognitionresult *result, Nserror *error) { typeof(weakself) __strong strongself =weakself; if(strongself.inrecognizer) {if(Error! =Nil) {[Strongself Endrecognizer]; } Else if(Result! =Nil) { if(strongself.inrecognizer) {Strongself.currenttext=[[Result besttranscription] formattedstring]; } if([result isfinal]) {[Strongself endrecognizer]; } } } if(Error) {}}]; Self.inrecognizer=YES;}-(void) endrecognizer{if(Self.currentText.length >0) { if(Self.haverecognizerspeaktextblock) {Self.haverecognizerspeaktextblock (Self.currenttext, Self.beginListen Location, cfabsolutetimegetcurrent ()-self.beginlistenlocation); }} Self.inrecognizer=NO; if(self.currenttask) {[Self.currenttask cancel]; Self.currenttask=Nil; } if(self.currentrequest) {[Self.currentrequest endaudio]; Self.currentrequest=Nil; } Self.currenttext=@"";}-(void) SpeechRecognizer: (Sfspeechrecognizer *) SpeechRecognizer Availabilitydidchange: (BOOL) available{Self.inrecognizer= !available;}-(void) Givebuffer: (cmsamplebufferref) buffer{if(self.currentrequest) {[Self.currentrequest appendaudiosamplebuffer:buffer]; } if(Self.inrecognizer && (Cfabsolutetimegetcurrent ()-self.beginlistenlocation) > -) {[Self endrecognizer]; [Self startrecognizer]; }}@end
IOS10 native Voice to text function