IOS10 native Voice to text function

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.