1.下载sdk
从讯飞官网下载sdk,下载完毕后是一个压缩包,本身就是一个可运行demo,结构如下:
doc:开发文档,包括pdf新手指南和接口说明的docset;
lib:为sdk的framework,也可以理解为静态库;
sample:demo代码,目录结果可以doc目录里的readme.txt,其中Definition.h文件是sdk对应的appid,一定要明白appid跟当前下载的静态库是捆绑的,不能随便乱搭配。
注意:重要的事情说三遍,我们不支持cocoapods,sdk必须要从官网下载,appid必须跟当前静态库匹配。
2.辅助库添加
不知道从什么时候开始,xcode集成framwork变得不太友好了,要求大家配置的信息有点多。
在集成静态库时,要需要大家集成libz.tbd库,如果你使用离线识别,还需要增加libc++.tbd。
3. framewor集成
你可以选择拖拽,或者通过add方式添加framework,但是还需要在build setting里配置framework search paths,这个path可以是绝对路径,也可以是相对路径,总之这个路径比如跟你的framework能够对的上才行,以demo为例,这个path为相对路径,
MSCDemo.xcodeproj是demo工程文件,
control
sample/MSCDemo/MSCDemo.xcodeproj
lib/iflyMSC.framework
那么path为$(SRCROOT)/../../lib,也就是说工程文件的上两级目录的lib下为framework,以这个准备来灵活设置你的实际工程配置。
集成好后,也可以在build pharses里选择link binary with libraries里看看是否有iflyMSC.framework,可以右键选择show in finder,看看实际位置与你的预期一致。
4.一般问题分析
1)报错10407,看到这个错误码,你要确认两个方面:
2)framework集成报错
看到这个编译错误,你不要首先怀疑sdk问题,而是要按照上面步骤来检查自己的配置是否合乎要求。
-----------使用---------
导入头文件
从迅飞开发者平台下载SDK,和导入SDK所要的库。引入头文件,以及遵从代理。
#import <UIKit/UIKit.h> #import "iflyMSC/iflyMSC.h" #import "iflyMSC/IFlySpeechRecognizerDelegate.h" #import "iflyMSC/IFlySpeechRecognizer.h" @interface ViewController : UIViewController<IFlySpeechRecognizerDelegate> @property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer; //不带界面的识别对象 @end
初使化AppId
注意下载的SDK要和AppId相对应。
-(void)initAppId { NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"567f981a"]; [IFlySpeechUtility createUtility:initString]; }
设置识别参数
-(void)initRecognizer { //单例模式,无UI的实例 if (_iFlySpeechRecognizer == nil) { _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance]; } // 设置参数 if (_iFlySpeechRecognizer != nil) { //扩展参数 [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]]; //设置听写模式 [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]]; //设置最长录音时间 [_iFlySpeechRecognizer setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]]; //设置后端点 [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]]; //设置前端点 [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]]; //网络等待时间 [_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]]; //设置采样率,推荐使用16K [_iFlySpeechRecognizer setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; //设置语言 [_iFlySpeechRecognizer setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]]; //设置方言 [_iFlySpeechRecognizer setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]]; //设置是否返回标点符号 [_iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]]; //设置数据返回格式 [_iFlySpeechRecognizer setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]]; } // 设置代理 _iFlySpeechRecognizer.delegate = self; }
实现代理协议
// 出现错误 - (void) onError:(IFlySpeechError *) error { NSLog(@"出现错误 :%@",error); } // 识别结果 - (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]]; } // 忽略结束。号 if ([result hasPrefix:@"\n\n------ 。"]) { return; } NSLog(@"%@",result); }
点击开始和结束识别
- (IBAction)understand:(id)sender { // 开始识别 [_iFlySpeechRecognizer startListening]; } - (IBAction)finish:(id)sender { // 停止识别 [_iFlySpeechRecognizer stopListening]; }
初使化
- (void)viewDidLoad { [super viewDidLoad]; [self initAppId];
[self initRecognizer];
}