iOS: 讯飞语音的使用
一、介紹:
訊飛語音做的相當(dāng)不錯,容錯率達(dá)到90%多,如果需要做語音方面的功能,它絕對是一個不錯的選擇。訊飛語音的功能很多:語音聽寫、語音識別、語音合成等,但我們最常用的還是語音聽寫。訊飛語音中包含界面的語音聽寫和不帶界面的語音聽寫,下面我來演示一下。
?
二、準(zhǔn)備工作:
(1)去訊飛語音開發(fā)平臺注冊賬號并登陸,然后在控制臺創(chuàng)建應(yīng)用,獲取對應(yīng)的app id,這個以后使用它注冊激活訊飛語音。
(2)下載訊飛語音SDK,將其拖入到項目中,然后添加需要所有的依賴庫,另外還有新添加庫Contacts.framework,編譯運(yùn)行即可。
(3)引入頭文件,以及遵從代理,激活訊飛語音,并實(shí)現(xiàn)功能。
?
三、BitCode?設(shè)置
在?Xcode 7?默認(rèn)開啟了?Bitcode,Bitcode?需要工程依賴的類庫同時支持。而語記?SDK?暫時還不支持Bitcode,所以可以先臨時關(guān)閉。后續(xù)支持?Bitcode?請關(guān)注訊飛開放平臺版本更新,QQ?群中也會?醒。在?Targets - Build Settings?中搜索?Bitcode?即可,找 到相應(yīng)選項,設(shè)置為?NO。
?
四、演示:
第一種不帶界面的語音聽寫:
DDHomeSearchViewController.m
#import <UIKit/UIKit.h> #import "iflyMSC/iflyMSC.h" #import "iflyMSC/IFlySpeechRecognizerDelegate.h" #import "iflyMSC/IFlySpeechRecognizer.h"@interface DDHomeSearchViewController : UIViewController<IFlySpeechRecognizerDelegate> @property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer; //不帶界面的識別對象 @end//初始化Appid -(void)initAppId {NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];[IFlySpeechUtility createUtility:initString]; }//初始化識別器 -(void)initRecognizer {//單例模式,無UI的實(shí)例if (_iFlySpeechRecognizer == nil) {_iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];}// 設(shè)置參數(shù)if (_iFlySpeechRecognizer != nil) {//擴(kuò)展參數(shù)[_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];//設(shè)置聽寫模式[_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];//設(shè)置最長錄音時間[_iFlySpeechRecognizer setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];//設(shè)置后端點(diǎn)[_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];//設(shè)置前端點(diǎn)[_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];//網(wǎng)絡(luò)等待時間[_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];//設(shè)置采樣率,推薦使用16K[_iFlySpeechRecognizer setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];//設(shè)置語言[_iFlySpeechRecognizer setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];//設(shè)置方言[_iFlySpeechRecognizer setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];//設(shè)置是否返回標(biāo)點(diǎn)符號[_iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];//設(shè)置數(shù)據(jù)返回格式[_iFlySpeechRecognizer setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];}// 設(shè)置代理_iFlySpeechRecognizer.delegate = self; }//實(shí)現(xiàn)代理方法 // 出現(xiàn)錯誤 - (void) onError:(IFlySpeechError *) error {NSLog(@"出現(xiàn)錯誤 :%@",error); }// 識別結(jié)果 - (void) onResults:(NSArray *) results isLast:(BOOL)isLast {NSMutableString *result = [[NSMutableString alloc] init];NSDictionary *dic = [results objectAtIndex:0];for (NSString *key in dic) {[result appendFormat:@"\n\n------ %@ (置信度:%@) ------ \n\n",key,[dic objectForKey:key]];}// 忽略結(jié)束。號if ([result hasPrefix:@"\n\n------ 。"]) {return;}NSLog(@"%@",result); }//點(diǎn)擊開始和結(jié)束 - (IBAction)understand:(id)sender { // 開始識別 [_iFlySpeechRecognizer startListening]; }- (IBAction)finish:(id)sender { // 停止識別 [_iFlySpeechRecognizer stopListening]; }//初始化 - (void)viewDidLoad {[super viewDidLoad];[self initAppId];[self initRecognizer]; }演示結(jié)果:
2015-12-31 01:09:15.687 fa[23747:446296] [IFLYTEK] -[IFlySpeechUtility login],login=0 2015-12-31 01:09:15.720 fa[23747:446296] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null) 2015-12-31 01:09:15.723 fa[23747:446296] [IFLYTEK] login -[IFlyLoginParam toString:] SessionParam:timeout=20000,appid=567f981a,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,work_dir=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Data/Application/4C7C4E0A-9A1A-4063-8068-B8978BC7D946/Library/Caches,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:15.723 fa[23747:446296] [IFLYTEK] paramString: timeout=20000,appid=567f981a,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,work_dir=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Data/Application/4C7C4E0A-9A1A-4063-8068-B8978BC7D946/Library/Caches,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] login in 2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] +[IFlySpeechRecognizer sharedInstance][IN],iflyInstance=0 2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] +[IFlySpeechRecognizer sharedInstance][OUT],iflyInstance=53658592 2015-12-31 01:09:20.069 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening][IN] 2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] isr SessionParam:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] params=engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] grammarid=(null) 2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] audioSource=1 2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] isListening:NO 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening:grammar:][IN] 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count0=1 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] iflySpeechQueue0=1 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count2=3 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count3=2 2015-12-31 01:09:20.071 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer main][IN],self=53528272 2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] iflySpeechQueue1=3 2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening:grammar:][OUT] 2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] params: engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] startListening audioSource:1 2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer runService][OUT] 2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening][OUT] 2015-12-31 01:09:20.072 fa[23747:446340] [IFLYTEK] Converted String : 2016/12/31 01:09:20 072 2015-12-31 01:09:20.073 fa[23747:446296] [IFLYTEK] -[IFlyMscRecognizer startRecord][IN] 2015-12-31 01:09:20.073 fa[23747:446296] [IFLYTEK] +[IFlyAudioSession initRecordingAudioSession] 2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder setSample:],rate=16000 2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null) 2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder start],[IN] 2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] pcmRecorder|timeValue:0.049352 2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder start],[OUT],ret =0 2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] -[IFlyMscRecognizer startRecord][OUT] 2015-12-31 01:09:20.124 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer beginSession][IN],self= 2015-12-31 01:09:20.124 fa[23747:446340] [IFLYTEK] beginSession before state=1 2015-12-31 01:09:20.127 fa[23747:446340] [IFLYTEK] QISRSessionBegin:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi 2015-12-31 01:09:20.200 fa[23747:446340] [IFLYTEK] QISRRegisterNotify=0 2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] beginSession after state=1,_error=0 2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer beginSession][OUT],self=53528272 2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] _startDate,50338112 2015-12-31 01:09:20.205 fa[23747:446353] [IFLYTEK] recog_status_callback type==1,status=79 2015-12-31 01:09:20.383 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=1 2015-12-31 01:09:22.946 fa[23747:446353] [IFLYTEK] recog_result_callback resultStatus==0,result=47889584,resultLen=6 2015-12-31 01:09:22.946 fa[23747:446353] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0 2015-12-31 01:09:22.947 fa[23747:446296] ------ 中國 (置信度:100) ------ 2015-12-31 01:09:24.496 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=3 2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop][IN] 2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueFlush 2015-12-31 01:09:24.497 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=3 2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] stop recording success 2015-12-31 01:09:24.498 fa[23747:446353] [IFLYTEK] recog_result_callback resultStatus==5,result=50332544,resultLen=3 2015-12-31 01:09:24.498 fa[23747:446353] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0 2015-12-31 01:09:24.521 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueStop 2015-12-31 01:09:24.542 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueDispose 2015-12-31 01:09:24.542 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop][OUT] 2015-12-31 01:09:24.546 fa[23747:446340] [IFLYTEK] while end,self=53528272 2015-12-31 01:09:24.546 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][IN] 2015-12-31 01:09:24.547 fa[23747:446340] [IFLYTEK] info:{"rec_ustop":"0","app_fau":310,"app_start":"2016/12/31 01:09:20 072","app_frs":2875,"rec_close":4425,"app_lrs":4426,"rec_start":307,"rec_open":1,"app_ssb":52,"rec_ready":52,"app_lau":4425} 2015-12-31 01:09:24.547 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][OUT] 2015-12-31 01:09:24.548 fa[23747:446340] [IFLYTEK] -[ISREngine stopEngine] _szSession=53715544[IN] 2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd IN] 2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlySpeechRecognizer onError:] [IN] errorCode=0 2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlySpeechRecognizer onError:][OUT] 2015-12-31 01:09:24.549 fa[23747:446296] 出現(xiàn)錯誤 :<IFlySpeechError: 0x7fad02f74d80> 2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd OUT] 2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer main][OUT],self=53528272 2015-12-31 01:09:24.550 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer dealloc][IN],self=53528272 2015-12-31 01:09:24.550 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer dealloc][OUT],self=53528272第二種帶界面的語音聽寫:
DDHomeSearchViewController.m
// // DDHomeSearchViewController.m// // Created by 夏遠(yuǎn)全 on 16/12/1. // Copyright ? 2016年 . All rights reserved. // #import "DDHomeSearchController.h"@interface DDHomeSearchViewController ()<UISearchBarDelegate,IFlyRecognizerViewDelegate> {IFlyRecognizerView *_iflyRecognizerView; } @property (nonatomic, strong) UISearchBar * searchBar; @end@implementation DDHomeSearchController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor clearColor];//初始化導(dǎo)航欄 [self _createNavigationBar];//初始化語音識別 [self initIFly];[self initAppid]; }//創(chuàng)建導(dǎo)航欄 - (void)_createNavigationBar {//取消self.navigationItem.leftBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) ImageName:@"back" Title:@"取消" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(back)];_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];//搜索欄_searchBar.placeholder = @"請輸入要搜索的商家、品類";_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;_searchBar.autoresizesSubviews = YES;_searchBar.delegate = self;self.navigationItem.titleView = _searchBar;//搜索self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"語音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)]; }//返回 -(void)back{[self.presentingViewController dismissViewControllerAnimated:NO completion:nil]; }//初始化Appid -(void)initAppid{//注冊訊飛語音NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];[IFlySpeechUtility createUtility:initString]; }//語音識別初始化 -(void)initIFly{//單例模式,無UI的實(shí)例if (_iflyRecognizerView == nil) {_iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];_iflyRecognizerView.delegate = self;}// 設(shè)置參數(shù)if (_iflyRecognizerView != nil) {//擴(kuò)展參數(shù)[_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];//設(shè)置聽寫模式[_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];//設(shè)置最長錄音時間[_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];//設(shè)置后端點(diǎn)[_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];//設(shè)置前端點(diǎn)[_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];//網(wǎng)絡(luò)等待時間[_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];//設(shè)置采樣率,推薦使用16K[_iflyRecognizerView setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];//設(shè)置語言[_iflyRecognizerView setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];//設(shè)置方言[_iflyRecognizerView setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];//設(shè)置是否返回標(biāo)點(diǎn)符號[_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];//設(shè)置數(shù)據(jù)返回格式[_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];} }//語音搜索啟動 -(void)voiceStart{//按鈕變換self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"結(jié)束" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceEnd)];//啟動識別服務(wù) [_iflyRecognizerView start]; }//結(jié)束錄音 -(void)voiceEnd{//按鈕變換self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"語音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)];//結(jié)束識別服務(wù) [_iflyRecognizerView cancel]; }/*識別結(jié)果返回代理@param resultArray 識別結(jié)果@ param isLast 表示是否最后一次結(jié)果*/ - (void)onResult: (NSArray *)resultArray isLast:(BOOL) isLast {[resultArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {DDLog(@"%@",obj);}]; } /*識別會話錯誤返回代理@ param error 錯誤碼*/ - (void)onError: (IFlySpeechError *) error {DDLog(@"語音識別出錯:%@",error); }#pragma mark - UISearchBarDelegate -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{[searchBar becomeFirstResponder]; } -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{DDLog(@"%@",searchText); } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{[searchBar resignFirstResponder];[searchBar setText:nil]; } @end演示結(jié)果:
2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] startListening audioSource:1 2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlySpeechRecognizer runService][OUT] 2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlySpeechRecognizer startListening][OUT] 2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp start][OUT] 2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView start],identifyingVIew start 2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView start][OUT] 2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyMscRecognizer startRecord][IN] 2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] +[IFlyAudioSession initRecordingAudioSession] 2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder setSample:],rate=16000 2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null) 2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder start],[IN] 2016-12-02 08:56:49.636 BiaoJiePay[1057:379880] [IFLYTEK] pcmRecorder|timeValue:0.187140 2016-12-02 08:56:49.641 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder start],[OUT],ret =0 2016-12-02 08:56:49.641 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyMscRecognizer startRecord][OUT] 2016-12-02 08:56:49.641 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer beginSession][IN],self= 2016-12-02 08:56:49.641 BiaoJiePay[1057:379975] [IFLYTEK] beginSession before state=1 2016-12-02 08:56:49.642 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onBeginOfSpeech][IN] 2016-12-02 08:56:49.642 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onBeginOfSpeech][OUT] 2016-12-02 08:56:49.654 BiaoJiePay[1057:379975] [IFLYTEK] QISRSessionBegin:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=default,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1115.1145,xxxxxx,xxxxxxxxx........ 2016-12-02 08:56:49.658 BiaoJiePay[1057:379975] [IFLYTEK] QISRRegisterNotify=0 2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] beginSession after state=1,_error=0 2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer beginSession][OUT],self=221654928 2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] _startDate,156534080 2016-12-02 08:56:49.790 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==0,status=1 2016-12-02 08:56:50.823 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==1,status=1175 2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] recog_result_callback resultStatus==0,result=??μ??μ,resultLen=6 2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0 2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] -[ISRDataHander dataHander:parse:],resultString=呵呵 2016-12-02 08:56:51.850 BiaoJiePay[1057:379880] {"\U5475\U5475" = 100; } 2016-12-02 08:56:52.326 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==0,status=3 2016-12-02 08:56:52.326 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop][IN] 2016-12-02 08:56:52.327 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueFlush 2016-12-02 08:56:52.330 BiaoJiePay[1057:379880] [IFLYTEK] stop recording success 2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] recog_result_callback resultStatus==5,result=?o?,resultLen=3 2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0 2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] -[ISRDataHander dataHander:parse:],resultString=! 2016-12-02 08:56:52.351 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueStop 2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueDispose 2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop][OUT] 2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onEndOfSpeech][IN] 2016-12-02 08:56:52.374 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onEndOfSpeech][OUT] 2016-12-02 08:56:52.374 BiaoJiePay[1057:379880] {"\Uff01" = 100; } 2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] while end,self=221654928 2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][IN] 2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] info:{"rec_close":2883,"app_ssb":198,"app_fau":346,"rec_ready":198,"ui_lrs":2931,"app_start":"2016/12/02 08:56:49 443","app_frs":2407,"app_lau":2883,"rec_ustop":"0","rec_start":343,"app_lrs":2903,"ui_frs":2407,"rec_open":2} 2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][OUT] 2016-12-02 08:56:52.381 BiaoJiePay[1057:379975] [IFLYTEK] -[ISREngine stopEngine] _szSession=221656648[IN] 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd IN] 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlySpeechRecognizer onError:] [IN] errorCode=0 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlySpeechRecognizer onError:][OUT] 2016-12-02 08:56:52.383 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onError:][IN],errorCode=0 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd OUT] 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer main][OUT],self=221654928 2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer dealloc][IN],self=221654928 2016-12-02 08:56:52.384 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer dealloc][OUT],self=221654928 2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView onError:][IN],errorCode=0 2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] 語音識別出錯:<IFlySpeechError: 0x10d39b2f0> 2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView onError:][OUT] 2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onError:][OUT]效果截圖:(說一句“附近的商店”)
?
參考:http://lecason.com/2015/12/21/iOS-Xunfei/
轉(zhuǎn)載于:https://www.cnblogs.com/XYQ-208910/p/6124418.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的iOS: 讯飞语音的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring事务管理Transactio
- 下一篇: 【备忘录】创建数字数组