一.集成
-
首先到 http://www.xfyun.cn 注冊并登陸下
1 創(chuàng)建新應(yīng)用(獲得后續(xù)的appid以及開通服務(wù))
2 得到appid,點(diǎn)擊開通服務(wù)選擇->語音聽寫,自動跳轉(zhuǎn)下載sdk界面
-
新建工程開始集成
1.添加系統(tǒng)庫<libz.tbd,AVFoundation.framework,SystemConfiguration.framework,Foundation.framework,CoreTelephoney.framework,AudioToolbox.framework,UIKit.framework,CoreLocation.framework,AddressBook.framework,AddressBook.framework,AddressBook.framework>
2.添加iflyMSC.framework(在下載的ZIP->lib 目錄下)
***
二.初始化
1.AppDelegate添加如下代碼
//設(shè)置sdk的log等級论寨,log保存在下面設(shè)置的工作路徑中
[IFlySetting setLogFile:LVL_ALL];
// 打開輸出在console的log開關(guān)
[IFlySetting showLogcat:YES];
//設(shè)置sdk的工作路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
[IFlySetting setLogFilePath:cachePath];
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"573b1d27"];
[IFlySpeechUtility createUtility:initString];
2.訊飛的語音識別主要有2個類 **IFlySpeechRecognizer(不帶界面)** 和 **IFlyRecognizerView(界面)**,用法差不多,主要介紹帶界面的用法.
- (IFlyRecognizerView *)iflyRecognizerView{
if (!_iflyRecognizerView) {
IFlyRecognizerView *iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
iflyRecognizerView.delegate = self;
[iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
//設(shè)置聽寫模式
[iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
IATConfig *instance = [IATConfig sharedInstance];
//設(shè)置最長錄音時間
[iflyRecognizerView setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//設(shè)置后端點(diǎn)
[iflyRecognizerView setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
//設(shè)置前端點(diǎn)
[iflyRecognizerView setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
//網(wǎng)絡(luò)等待時間
[iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
//設(shè)置采樣率打瘪,推薦使用16K
[iflyRecognizerView setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
if ([instance.language isEqualToString:[IATConfig chinese]]) {
//設(shè)置語言
[iflyRecognizerView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
//設(shè)置方言
[iflyRecognizerView setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
}else if ([instance.language isEqualToString:[IATConfig english]]) {
//設(shè)置語言
[iflyRecognizerView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
}
//設(shè)置是否返回標(biāo)點(diǎn)符號
[iflyRecognizerView setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];
self.iflyRecognizerView = iflyRecognizerView;
}
return _iflyRecognizerView;
}
````
然后啟動服務(wù) [self.iflyRecognizerView start];
3 然后通過代理 IFlyRecognizerViewDelegate 該代理就2個方法如下
/*!
* 回調(diào)返回識別結(jié)果
*
* @param resultArray 識別結(jié)果朴沿,NSArray的第一個元素為NSDictionary虎忌,NSDictionary的key為識別結(jié)果罩缴,sc為識別結(jié)果的置信度
* @param isLast -[out] 是否最后一個結(jié)果
*/
- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast;
/*!
* 識別結(jié)束回調(diào)
*
* @param error 識別結(jié)束錯誤碼
*/
- (void)onError: (IFlySpeechError *) error;
三.代理模式到Block的轉(zhuǎn)換(簡單封裝)
1.抽取一個工具類,通過block傳遞結(jié)果
/**
* 回調(diào)
*/
typedef void (^RecognizerBlock)(IFlySpeechError *error,NSString *result);
@interface ZZiflyTool : NSObject
+ (instancetype)shareTool;
- (void)startRecognizer:(RecognizerBlock)block;
@end
外界只需要一句話就可以調(diào)用了
[[ZZiflyTool shareTool] startRecognizer:^(IFlySpeechError *error, NSString *result) {
if (error) {
[self showMes:error.errorDesc];
return;
}
NSString *str = result.length ? result:@"沒聽到你說啥";
[self showMes:str];
}];