iOS语音识别(科大讯飞SDK)
来源:互联网 发布:cydia出现数据库黄字 编辑:程序博客网 时间:2024/05/20 09:22
最近在研究语音识别,前段时间使用了百度语音识别,最近公司又突然让转用科大讯飞的SDK,感觉有点坑。不过也算是学习了使用新的SDK了吧。在这里将集成SDK以及一些使用方法写一下。
第一步:获取APPid
先在科大讯飞语音识别开发者网站注册,获取APPid。
第二步:集成SDK
导入科大讯飞的iflyMSC.framework;然后再导入相关的依赖库:
第三步:初始化启动服务
在app delegate里面写入如下代码初始化
NSLog(@"%@",[IFlySettinggetVersion]);
//设置sdk的log等级,log保存在下面设置的工作路径中
[IFlySettingsetLogFile:LVL_ALL];
//打开输出在console的log开关
[IFlySettingshowLogcat:NO];
//设置sdk的工作路径
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUser DomainMask,YES);
NSString *cachePath = [pathsobjectAtIndex:0];
[IFlySettingsetLogFilePath:cachePath];
//创建语音配置,appid必须要传入,仅执行一次则可
NSString *initString = [[NSStringalloc] initWithFormat:@"appid=%@",APPID_VALUE];
//所有服务启动前,需要确保执行createUtility
[IFlySpeechUtilitycreateUtility:initString];
在使用的页面
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%s",__func__);
[superviewWillAppear:animated];
[selfinitRecognizer];
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"%s",__func__);
[superviewWillDisappear:animated];
[_iFlySpeechRecognizercancel];
[_iFlySpeechRecognizersetDelegate:nil];
[_iFlySpeechRecognizersetParameter:@""forKey:[IFlySpeechConstantPARAMS]];
}
//初始化识别参数
- (void)initRecognizer
{
NSLog(@"%s",__func__);
//单利模式无UI的实例
if (self.iFlySpeechRecognizer==nil) {
_iFlySpeechRecognizer=[IFlySpeechRecognizersharedInstance];
[_iFlySpeechRecognizersetParameter:@""forKey:[IFlySpeechConstantPARAMS]];
//设置听写模式
[_iFlySpeechRecognizersetParameter:@"iat"forKey:[IFlySpeechConstantIFLY_DOM AIN]];
}
_iFlySpeechRecognizer.delegate=self;
if (_iFlySpeechRecognizer!=nil) {
IATConfig *instance=[IATConfigsharedInstance];
//设置最长录音时间
[_iFlySpeechRecognizersetParameter:instance.speechTimeoutforKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//设置后端点
[_iFlySpeechRecognizersetParameter:instance.vadEosforKey:[IFlySpeechConstantVAD_EOS]];
//设置前端点
[_iFlySpeechRecognizersetParameter:instance.vadBosforKey:[IFlySpeechConstantVAD_BOS]];
//网络等待时间
[_iFlySpeechRecognizersetParameter:@"20000"forKey:[IFlySpeechConstantNET_TI MEOUT]];
//设置采样率,推荐16K
[_iFlySpeechRecognizersetParameter:IATConfig.lowSampleRateforKey:[IFlySpeechConstant SAMPLE_RATE]];
if ([instance.languageisEqualToString:[IATConfigchinese]]) {
//设置语言
[_iFlySpeechRecognizersetParameter:instance.languageforKey:[IFlySpeechConstant LANGUAGE]];
//设置方言
[_iFlySpeechRecognizersetParameter:instance.accentforKey:[IFlySpeechConstant ACCENT]];
}elseif([instance.languageisEqualToString:[IATConfigenglish]]){
[_iFlySpeechRecognizersetParameter:instance.languageforKey:[IFlySpeechConstant LANGUAGE]];
}
//设置是否返回标点符号
[_iFlySpeechRecognizersetParameter:instance.dotforKey:[IFlySpeechConstantAS R_PTT]];
}
}
- (void)startButtonClick
{
NSLog(@"start");
[_textViewsetText:@""];
[_textViewresignFirstResponder];
self.isCanceled=NO;
if (_iFlySpeechRecognizer==nil) {
[selfinitRecognizer];
}
[_iFlySpeechRecognizercancel];
//设置音频来源为麦克风
[_iFlySpeechRecognizersetParameter:IFLY_AUDIO_SOURCE_MICforKey:@"audio_source"];
//设置听说结果格式为json
[_iFlySpeechRecognizersetParameter:@"json"forKey:[IFlySpeechConstantRESULT_ TYPE]];
//保存录音文件,保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下(为了测试音频流识别用的)
[_iFlySpeechRecognizersetParameter:@"asr.pcm"forKey:[IFlySpeechConstantASR_ AUDIO_PATH]];
[_iFlySpeechRecognizersetDelegate:self];
BOOL ret=[_iFlySpeechRecognizerstartListening];
if (ret) {
NSLog(@"启动成功");
}else{
[_popUpViewshowText:@"启动失败"];
}
}
//停止录音
- (void)stopButtonClick
{
NSLog(@"stop");
[_iFlySpeechRecognizerstopListening];
[_textViewresignFirstResponder];
}
//取消录音
- (void)cancelButtonClick
{
NSLog(@"cancel");
[_iFlySpeechRecognizercancel];
[_textViewresignFirstResponder];
}
#pragma mark - IFlySpeechRecognizerDelegate
/**
音量回调函数
volume 0-30
****/
- (void) onVolumeChanged: (int)volume
{
if (self.isCanceled) {
[_popUpViewremoveFromSuperview];
return;
}
NSString * vol = [NSStringstringWithFormat:@"音量:%d",volume];
[_popUpViewshowText: vol];
}
/**
开始识别回调
****/
- (void) onBeginOfSpeech
{
NSLog(@"onBeginOfSpeech");
[_popUpViewshowText: @"正在录音"];
}
/**
停止录音回调
****/
- (void) onEndOfSpeech
{
NSLog(@"onEndOfSpeech");
[_popUpViewshowText: @"停止录音"];
}
/**
听写结束回调(注:无论听写是否正确都会回调)
error.errorCode =
0 听写正确
other 听写出错
****/
- (void)onError:(IFlySpeechError *)error
{
NSLog(@"%s",__func__);
NSString *text;
if (self.isCanceled) {
text=@"识别取消";
}elseif (error.errorCode==0){
if (self.result.length==0) {
text=@"无识别结果";
}else{
text=@"识别成功";
}
}else{
text=[NSStringstringWithFormat:@"发生错误:%d %@",error.errorCode,error.errorDesc];;
NSLog(@"%@",text);
}
}
/**
无界面,听写结果回调
results:听写结果
isLast:表示最后一次
****/
- (void)onResults:(NSArray *)results isLast:(BOOL)isLast
{
NSMutableString *resultString=[[NSMutableStringalloc]init];
NSDictionary *dic=results[0];
for (NSString *keyin dic) {
[resultString appendFormat:@"%@",key];
}
self.result=[NSStringstringWithFormat:@"%@%@",self.textView.text,resultString];
NSString *resultFromJson=[ISRDataHelperstringFromJson:resultString];
self.textView.text=[NSStringstringWithFormat:@"%@%@",_textView.text,resultFromJson];
if (isLast) {
NSLog(@"听说结果(json):%@测试",self.result);
}
NSLog(@"_result=%@",_result);
NSLog(@"resultFromJson=%@",resultFromJson);
NSLog(@"isLast=%d,_textView.text=%@",isLast,_textView.text);
}
/**
听写取消回调
****/
- (void) onCancel
{
NSLog(@"识别取消");
}
- iOS语音识别(科大讯飞SDK)
- ios开发-语音识别(科大讯飞)
- 中国移动灵犀云语音识别及合成SDK(iOS)使用指南
- Unity接入语音SDk 语音识别
- 百度语音识别SDK使用方法
- 安装百度语音识别sdk
- iOS——科大讯飞之语音识别
- ios开发科大讯飞的语音识别
- google语音api识别(ios)
- 【iOS开发】---- 语音识别
- ios 语音识别
- iOS语音识别封装
- iOS 语音识别
- iOS-百度语音识别
- ROS下使用科大讯飞SDK进行在线语音识别
- 使用科大讯飞最新版SDK在树莓派上搭建语音识别服务
- 使用科大讯飞提供的SDK实现android语音识别
- 中国移动灵犀云语音识别SDK(android)使用指南
- React Native商城项目实战16 - 购物中心详细页
- 机器学习工程师需要了解的十种算法
- android SpannableStringBuilder设置自定义字体
- 9、Git
- 利用项目管理软件简单解决金融软件外包管理难题
- iOS语音识别(科大讯飞SDK)
- 什么是JSONP
- ZABBIX API
- Hibernate 参数含有中文时查询结果为空
- 1023. Have Fun with Numbers (20)
- 安卓代码访问互联网
- I00038 自守数(Automorphic number)
- Leetcode 389. Find the Difference 找不同 解题报告
- POJ 1458Common Subsequence