接入讯飞SDK及参数设置
简要描述
主要协助云上越秀App接入讯飞SDK,并且设置讯飞相应参数的功能代码示例;
主要内容
讯飞SDK是以framework框架的方式提供SDK的接入。
SDK的版本:1.180版本。
包大小:19.2M。
引入方式:将framework文件夹拖入工程即可。
讯飞SDK初始化代码示例:
+ (void)configIFly {//Set log level[IFlySetting setLogFile:LVL_ALL];//Set whether to output log messages in Xcode console[IFlySetting showLogcat:YES];//Set the local storage path of SDKNSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachePath = [paths objectAtIndex:0];[IFlySetting setLogFilePath:cachePath];PHAIConfig *config = PHAIConfig.sharedConfig;if ([PHAIUtils isNilOrEmpty:config.appId]) {IFlyLog(@"App ID 为空,初始化讯飞SDK失败");}//Set APPIDNSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",config.appId];//Configure and initialize iflytek services.(This interface must been invoked in application:didFinishLaunchingWithOptions:)[IFlySpeechUtility createUtility:initString];}
1. 10s内无声音自动中断
讯飞的SDK文档里面已经说明此类情况:文档地址
在听写过程中如果10秒未说话录制会自动停止。
答:听写vad_eos为支持的最长静音时间,超过这个时间会认为音频结束自动断开。
代码上主要通过配置[_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];和[_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];来实现10s内无声音自动中断。
示例代码:
-(void)defaultSetting {_speechTimeout = @"60000";_vadEos = @"10000";_vadBos = @"10000";_dot = @"1";_sampleRate = @"16000";_language = CHINESE;_accent = PUTONGHUA;_haveView = NO;_accentNickName = [[NSArray alloc] initWithObjects:NSLocalizedString(@"K_LangCant", nil), NSLocalizedString(@"K_LangChin", nil), NSLocalizedString(@"K_LangEng", nil), NSLocalizedString(@"K_LangSzec", nil),NSLocalizedString(@"K_LangJapa", nil),NSLocalizedString(@"K_LangRuss", nil),NSLocalizedString(@"K_LangFren", nil),NSLocalizedString(@"K_LangSpan", nil),NSLocalizedString(@"K_LangKor", nil), nil];_isTranslate = NO;}- (IFlySpeechRecognizer *)iFlySpeechRecognizer {if (_iFlySpeechRecognizer == nil) {_iFlySpeechRecognizer = IFlySpeechRecognizer.sharedInstance;_iFlySpeechRecognizer.delegate = self;//设置识别参数//设置为听写模式[_iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];//Set result type[_iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]];//Set microphone as audio source// [_iFlySpeechRecognizer setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];PHAIIATConfig *instance = [PHAIIATConfig sharedInstance];//set timeout of recording[_iFlySpeechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];//set VAD timeout of end of speech(EOS)[_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];//set VAD timeout of beginning of speech(BOS)[_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];//set network timeout[_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];//set sample rate, 16K as a recommended option[_iFlySpeechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];//set language[_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];//set accent[_iFlySpeechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];//set whether or not to show punctuation in recognition results[_iFlySpeechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];}return _iFlySpeechRecognizer;}
2.监听讯飞录音状态
实现代理<IFlySpeechRecognizerDelegate>。
#pragma mark - IFlySpeechRecognizerDelegate/**volume callback,range from 0 to 30.**/- (void)onVolumeChanged: (int)volume {}/**Beginning Of Speech**/- (void)onBeginOfSpeech {IFlyLog(@"onBeginOfSpeech");}/**End Of Speech**/- (void)onEndOfSpeech {IFlyLog(@"onEndOfSpeech");}/**recognition session completion, which will be invoked no matter whether it exits error.error.errorCode =0 successother fail**/- (void)onCompleted:(IFlySpeechError *)error {IFlyLog(@"%s",__func__);self.result = nil;if (error.errorCode != 0) {IFlyLog(@"语音识别出现问题:%@", error.errorDesc);} else {PHAIFileManager *fileManager = PHAIFileManager.sharedManager;NSString *fileName = self.fileName;NSString *pcmFilePath = [fileManager getCachesPCMURLWithName:fileName].path;NSString *mp3FilePath = [fileManager getIATAudioFileWithName:fileName].path;int sampleRate = [PHAIIATConfig.sharedInstance.sampleRate intValue];__weak typeof(self) weakSelf = self;[PHAIMP3Encoder conventToMp3WithCafFilePath:pcmFilePathmp3FilePath:mp3FilePathsampleRate:sampleRatecallback:^(BOOL complete) {if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(mp3EnconderComplete:fileName:)]) {[weakSelf.delegate mp3EnconderComplete:complete fileName:fileName];}AudioEncoderLog(@"转码完成");}];}if (self.delegate && [self.delegate respondsToSelector:@selector(recongnizingComplete:)]) {[self.delegate recongnizingComplete:error.errorCode == 0];}}/**result callback of recognition without viewresults:recognition resultsisLast:whether or not this is the last result**/- (void)onResults:(NSArray *)results isLast:(BOOL)isLast {IFlyLog(@"%@",results);NSMutableString *resultString = [[NSMutableString alloc] init];NSDictionary *dic = results[0];for (NSString *key in dic) {[resultString appendFormat:@"%@",key];}if (self.result == nil) {self.result = @"";}NSString *result = [NSString stringWithFormat:@"%@%@", self.result, resultString];NSString * resultFromJson = nil;if([PHAIIATConfig sharedInstance].isTranslate) {NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData: //The result type must be utf8, otherwise an unknown error will happen.[resultString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];if(resultDic != nil){NSDictionary *trans_result = [resultDic objectForKey:@"trans_result"];if([[PHAIIATConfig sharedInstance].language isEqualToString:@"en_us"]){NSString *dst = [trans_result objectForKey:@"dst"];IFlyLog(@"dst=%@",dst);resultFromJson = [NSString stringWithFormat:@"%@\ndst:%@",resultString,dst];}else{NSString *src = [trans_result objectForKey:@"src"];IFlyLog(@"src=%@",src);resultFromJson = [NSString stringWithFormat:@"%@\nsrc:%@",resultString,src];}}}else{resultFromJson = [PHAIISRDataHelper stringFromJson:resultString];}self.result = [NSString stringWithFormat:@"%@%@",self.result, resultFromJson];// NSData *jsonData = [resultString dataUsingEncoding:NSUTF8StringEncoding];// NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];// if (json) {// [self.jsons addObject:json];// }if (resultString) {[self.jsons addObject:resultString];}if (isLast){IFlyLog(@"ISR Results(json):%@", result);}IFlyLog(@"result=%@", result);IFlyLog(@"resultFromJson=%@",resultFromJson);IFlyLog(@"isLast=%d,_textView.text=%@",isLast, self.result);if (self.delegate && [self.delegate respondsToSelector:@selector(recognizingResult:)]) {[self.delegate recognizingResult:self.result];}}/**callback of canceling recognition**/- (void)onCancel {self.result = nil;IFlyLog(@"Recognition is cancelled");}#pragma mark - Getter- (NSData *)data {if (self.jsons.count > 0) {NSError *error;NSData *data = [NSJSONSerialization dataWithJSONObject:self.jsons options:NSJSONWritingPrettyPrinted error:&error];if (error) {IFlyLog(@"讯飞语音识别json生成失败: %@", error);return nil;} else {return data;}} else {return nil;}}