Voice Features for iOS development

Source: Internet
Author: User

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

To create a voice configuration, AppID must be passed in and executed only once to

NSString *initstring = [[NSString alloc] initwithformat:@ "APPID=570F3DB3"];

Before all services are started, you need to ensure that createutility is executed

[Iflyspeechutility createutility:initstring];

return YES;

}

Create audio files using the Apple Native API framework. You need to add a header file to the Avfoundation framework, which will ask you to open this feature the first time the program is enabled. Use the Hkust API to use the online SDK only for free, and call the offline SDK to use only 3 iOS devices for 35 days. Offline purchase price is very expensive.

#import "Iflymsc/iflymsc.h"

#import "Iflymsc/iflyspeechconstant.h"

#import "Iflymsc/iflyspeechrecognizerdelegate.h"

#import "Iflymsc/iflyspeechrecognizer.h"

@import avfoundation;

<AVAudioRecorderDelegate> Use Agents

@property (Nonatomic,strong) Iflyspeechrecognizer *iflyspeechrecognizer;

@property (Nonatomic,strong) Avaudiorecorder *recorder;

@property (Nonatomic,strong) Avaudioplayer *player;

@property (Nonatomic,strong) Nstimer *timer;

@property (Nonatomic,strong) Nsurl *url;

@property (Nonatomic,strong) nsmutabledictionary *dict;

@property (Nonatomic,strong) Uiprogressview *gress;

@property (Nonatomic,strong) nsmutablestring *text;

@property (Weak, nonatomic) Iboutlet Uitextfield *textfield;

Avaudiosession *session = [Avaudiosession sharedinstance];

Singleton mode, set to record and play

[Session Setcategory:avaudiosessioncategoryplayandrecord Error:nil];

[Session Setactive:yes Error:nil];

NSString *str = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0];

Self.url = [Nsurl fileurlwithpath:[str stringbyappendingpathcomponent:@ "MYFIRSTRECORD.CAF"];

Setting the save path for a file

Self.gress = [[Uiprogressview Alloc]initwithprogressviewstyle:uiprogressviewstyledefault];

self.gress.progress = 0; Set progress bar

Self.gress.frame = CGRectMake (90, 200, 200, 2);

[Self.view addSubview:self.gress];

Self.dict = [Nsmutabledictionary dictionary];

[Self.dict setobject:@ (KAUDIOFORMATLINEARPCM) Forkey:avformatidkey];

[Self.dict setobject:@10000 Forkey:avsampleratekey];

[Self.dict setobject:@1 Forkey:avnumberofchannelskey];

[Self.dict setobject:@8 Forkey:avlinearpcmbitdepthkey];

[Self.dict setobject:@ (YES) Forkey:avlinearpcmisfloatkey];

if (!_recorder) {

_recorder = [[Avaudiorecorder alloc]initwithurl:self.url settings:self.dict Error:nil];

_recorder.delegate = self;

_recorder.meteringenabled = YES;

}

if (!_player) {

_player = [[Avaudioplayer alloc]initwithcontentsofurl:self.url Error:nil];

}

[Self start];

-(void) start{

_iflyspeechrecognizer = [Iflyspeechrecognizer sharedinstance]; Set Dictation mode

_iflyspeechrecognizer.delegate = self;

2. Set Dictation parameters

[_iflyspeechrecognizer Setparameter: @ "IAT" Forkey: [Iflyspeechconstant Ifly_domain]];

Asr_audio_path is the recording file name, set value to nil or null to cancel the save, the default save directory under Library/cache.

[_iflyspeechrecognizer setparameter:@ "ASRVIEW.PCM" Forkey:[iflyspeechconstant Asr_audio_path]];

3. Start the identification service [_iflyspeechrecognizer start];

}

-(Nstimer *) timer{

if (!_timer) {

_timer = [Nstimer scheduledtimerwithtimeinterval:1/20.0 target:self selector: @selector (showprogress:) userinfo:nil Repeats:yes];

NSLog (@ "Nstime");

}

return _timer;

}

-(void) ShowProgress: (nstimer*) t{

[Self.recorder Updatemeters];

float power = [Self.recorder averagepowerforchannel:0];//get the first channel of audio, note the audio intensity range 160 to 0

Self.gress.progress = (power+160)/160.0;

}

-(Ibaction) recording: (ID) Sender {

if (![ Self.recorder IsRecording]) {

[Self.recorder Record];

Self.timer.fireDate = [NSDate distantpast];

}

NSLog (@ "recording begins!");

}

-(Ibaction) pause: (ID) Sender {

if (![ Self.recorder IsRecording]) {

[Self.recorder pause];

Self.timer.fireDate = [NSDate distantfuture];

}else{

[Self.recorder Record];

Self.timer.fireDate = [NSDate distantpast];

}

NSLog (@ "pause toggle!");

}

-(Ibaction) STOP: (ID) Sender {

[Self.recorder stop];

NSLog (@ "recording end!");

Self.timer.fireDate = [NSDate distantfuture];

Self.timer = nil;

self.gress.progress = 0;

}

-(Ibaction) Iflyclick: (ID) Sender {

NSLog (@ "Iflyclick"); Call method when button is pressed touchdown

_text = [[nsmutablestring alloc]init];

[_iflyspeechrecognizer startlistening];

}

-(Ibaction) Iflyclickstop: (ID) Sender {

NSLog (@ "Iflyclickstop"); The method that is called after the button is left, whether it is left on the buttons or left outside the button. Touchupinside&touchupoutside

[_iflyspeechrecognizer stoplistening];

}

#pragma-mark-avaudiorecorddelegate

-(void) audiorecorderdidfinishrecording: (Avaudiorecorder *) Recorder successfully: (BOOL) flag{

Avaudiosession *session = [Avaudiosession sharedinstance];

[Session Setcategory:avaudiosessioncategoryplayback Error:nil]; Turn on the speaker

[Session Setactive:yes Error:nil];

if (![ Self.player IsPlaying]) {

[Self.player play];

}

NSLog (@ "recording completed!");

}

#pragma-mark-iflyspeechrecognizerdelegate

-(void) OnError: (Iflyspeecherror *) errorcode{

NSLog (@ "OnError-----------------%@", ErrorCode);

}

-(void) Onresults: (Nsarray *) results islast: (BOOL) islast{

NSLog (@ "Onresults-----------------%@", results);

nsmutablestring *result = [[Nsmutablestring alloc] init];

Nsdictionary *dic = [Results objectatindex:0];

For (NSString *key in dic) {

[Result appendformat:@ "%@", key];//merge result

}

NSLog (@ "result-------%@", result);

Nsmutablearray *strarr = [[Result componentsseparatedbystring:@ "\"}]} "] mutablecopy];

[Strarr Removelastobject];

For (NSString *str in Strarr) {

[_text appendstring:[[str componentsseparatedbystring:@ "\" "]lastobject]";

}

NSLog (@ "_text-------%@", _text);

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

Self.textField.text = _text;

});

}

-(void) Viewwilldisappear: (BOOL) animated

{

[_iflyspeechrecognizer Cancel];

_iflyspeechrecognizer.delegate = nil;

Set back non-semantic recognition

[_iflyspeechrecognizer destroy];

[Super viewwilldisappear:animated];

}

Voice Features for iOS development

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.