一、下载讯飞的SDK

二、集成流程

第一步:获取appid(进入控制台查看)

第二步:工程配置

1、将模板项目的lib文件拉入项目,并加入依赖库

注意: 添加iflyMSC.framework时,请检查工程BuildSetting中的framwork path的设置,如果出现找不到framework的情况,可以将path清空,在Xcode中删除framework,然后重新添加。 如果使用了离线识别,还需要增加libc++.tbd。 如果使用aiui,需要添加libc++.tbd,libicucord.tbd。

2、设置Bitcode

在Xcode 7,8默认开启了Bitcode,而Bitcode 需要工程依赖的所有类库同时支持。MSC SDK暂时还不支持Bitcode,可以先临时关闭。后续MSC SDK支持Bitcode 时,会在讯飞开放平台上进行SDK版本更新,QQ支持群中同时会有相关提醒,请关注。关闭此设置,只需在Targets - Build Settings 中搜索Bitcode 即可,找到相应选项,设置为NO。

3、用户隐私权限配置

iOS 10发布以来,苹果为了用户信息安全,加入隐私权限设置机制,让用户来选择是否允许。 隐私权限配置可在info.plist 新增相关privacy字段,MSC SDK中需要用到的权限主要包括麦克风权限、联系人权限和地理位置权限:

NSMicrophoneUsageDescription

NSLocationUsageDescription

NSLocationAlwaysUsageDescription

NSContactsUsageDescription

即在Info.plist 中增加下图设置:

第1种设置方法:

第2种设置方法:

第三步:初始化

初始化示例:

//可在启动时添加,Appid是应用的身份信息,具有唯一性,初始化时必须要传入Appid。

NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", @"YourAppid"];

[IFlySpeechUtility createUtility:initString];

注意: 初始化是一个异步过程,可放在App启动时执行初始化,具体代码可以参照Demo的MSCAppDelegate.m。

还需要在工程中添加结果解析文件,否则编译会报错。

文件在Demo中的位置:MSCDemo -> tools -> jsoncpp

还需要从模板项目中拖拽IATConfig类和ISRDataHelper类到自己的项目中

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

三、下面就是具体代码操作

1、语音在线合成

本示例对应Demo的TTSUIController文件,为在线合成的代码示例。

//包含头文件
#import "iflyMSC/IFlyMSC.h"
//需要实现IFlySpeechSynthesizerDelegate合成会话的服务代理
@interface TTSUIController : UIViewController<IFlySpeechSynthesizerDelegate>
@property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;
@end//获取语音合成单例
_iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
//设置协议委托对象
_iFlySpeechSynthesizer.delegate = self;
//设置合成参数
//设置在线工作方式
[_iFlySpeechSynthesizer setParameter:[IFlySpeechConstant TYPE_CLOUD]  forKey:[IFlySpeechConstant ENGINE_TYPE]];
//设置音量,取值范围 0~100
[_iFlySpeechSynthesizer setParameter:@"50" forKey: [IFlySpeechConstant VOLUME]];
//发音人,默认为”xiaoyan”,可以设置的参数列表可参考“合成发音人列表”
[_iFlySpeechSynthesizer setParameter:@" xiaoyan "forKey: [IFlySpeechConstant VOICE_NAME]];
//保存合成文件名,如不再需要,设置为nil或者为空表示取消,默认目录位于library/cache下
[_iFlySpeechSynthesizer setParameter:@" tts.pcm" forKey: [IFlySpeechConstant TTS_AUDIO_PATH]];
//启动合成会话
[_iFlySpeechSynthesizer startSpeaking: @"你好,我是科大讯飞的小燕"];//IFlySpeechSynthesizerDelegate协议实现
//合成结束
- (void) onCompleted:(IFlySpeechError *) error {}
//合成开始
- (void) onSpeakBegin {}
//合成缓冲进度
- (void) onBufferProgress:(int) progress message:(NSString *)msg {}
//合成播放进度
- (void) onSpeakProgress:(int) progress beginPos:(int)beginPos endPos:(int)endPos {}

2、语音听写(带界面的例子) IFlySpeechRecognizer是不带界面的语音听写控件,IFlyRecognizerView是带界面的控件,此处仅介绍不带界面的语音听写控件。在ViewController.m中写的,可以直接测试

使用示例如下所示:

//
//  ViewController.m
//  文字语音读写
//
//  Created by User on 2017/7/7.
//  Copyright © 2017年 User. All rights reserved.
//#import "ViewController.h"
#import "IATConfig.h"
#import "ISRDataHelper.h"#import "iflyMSC/iflyMSC.h"@interface ViewController ()<IFlyRecognizerViewDelegate>@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;//语音转文字//(讯飞自带的界面View)
@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//Recognition control with view@property (nonatomic, strong) NSMutableArray *resultARR;@end@implementation ViewController#pragma mark - Initialization/**initialize recognition conctol and set recognition params**/
-(void)initRecognizer
{NSLog(@"%s",__func__);//recognition singleton with viewif (_iflyRecognizerView == nil) {_iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];}[_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];//set recognition domain[_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];_iflyRecognizerView.delegate = self;if (_iflyRecognizerView != nil) {IATConfig *instance = [IATConfig sharedInstance];//set timeout of recording[_iflyRecognizerView setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];//set VAD timeout of end of speech(EOS)[_iflyRecognizerView setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];//set VAD timeout of beginning of speech(BOS)[_iflyRecognizerView setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];//set network timeout[_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];//set sample rate, 16K as a recommended option[_iflyRecognizerView 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[_iflyRecognizerView setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];if([[IATConfig sharedInstance].language isEqualToString:@"en_us"]){if([IATConfig sharedInstance].isTranslate){[self translation:NO];}}else{if([IATConfig sharedInstance].isTranslate){[self translation:YES];}}}}-(void)translation:(BOOL) langIsZh
{if ([IATConfig sharedInstance].haveView == NO) {[_iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_SCH]];if(langIsZh){[_iFlySpeechRecognizer setParameter:@"cn" forKey:@"orilang"];[_iFlySpeechRecognizer setParameter:@"en" forKey:@"translang"];}else{[_iFlySpeechRecognizer setParameter:@"en" forKey:@"orilang"];[_iFlySpeechRecognizer setParameter:@"cn" forKey:@"translang"];}[_iFlySpeechRecognizer setParameter:@"translate" forKey:@"addcap"];[_iFlySpeechRecognizer setParameter:@"its" forKey:@"trssrc"];}else{[_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_SCH]];if(langIsZh){[_iflyRecognizerView setParameter:@"cn" forKey:@"orilang"];[_iflyRecognizerView setParameter:@"en" forKey:@"translang"];}else{[_iflyRecognizerView setParameter:@"en" forKey:@"orilang"];[_iflyRecognizerView setParameter:@"cn" forKey:@"translang"];}[_iflyRecognizerView setParameter:@"translate" forKey:@"addcap"];[_iflyRecognizerView setParameter:@"its" forKey:@"trssrc"];}}- (void)viewDidLoad {[super viewDidLoad];_resultARR = [[NSMutableArray alloc]init];if(_iflyRecognizerView == nil){[self initRecognizer ];}//Set microphone as audio source[_iflyRecognizerView setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];//Set result type[_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];//Set the audio name of saved recording file while is generated in the local storage path of SDK,by default in library/cache.[_iflyRecognizerView setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];btn.backgroundColor = [UIColor redColor];btn.frame = CGRectMake(200, 300, 100, 60);[btn addTarget:self action:@selector(btnClick1:) forControlEvents:UIControlEventTouchUpInside];btn.selected = NO;[self.view addSubview:btn];[self setExclusiveTouchForButtons:self.view];}-(void)setExclusiveTouchForButtons:(UIView *)myView
{for (UIView * button in [myView subviews]) {if([button isKindOfClass:[UIButton class]]){[((UIButton *)button) setExclusiveTouch:YES];}else if ([button isKindOfClass:[UIView class]]){[self setExclusiveTouchForButtons:button];}}
}- (void)btnClick1:(UIButton *)btn{[_resultARR removeAllObjects];//启动识别服务[_iflyRecognizerView start];}/*!*  回调返回识别结果**  @param resultArray 识别结果,NSArray的第一个元素为NSDictionary,NSDictionary的key为识别结果,sc为识别结果的置信度*  @param isLast      -[out] 是否最后一个结果*/
- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast{NSMutableString *resultString = [[NSMutableString alloc] init];NSDictionary *dic = [resultArray objectAtIndex:0];NSLog(@"%@",dic);for (NSString *key in dic) {[resultString appendFormat:@"%@",key];}NSString * resultFromJson =  nil;if([IATConfig 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"];NSString *src = [trans_result objectForKey:@"src"];NSLog(@"src=%@",src);resultFromJson = [NSString stringWithFormat:@"%@\nsrc:%@",resultString,src];}}else{resultFromJson = [NSString stringWithFormat:@"%@",resultString];//;[ISRDataHelper stringFromJson:resultString];}[_resultARR addObject:resultFromJson];}/*!*  识别结束回调**  @param error 识别结束错误码*/
- (void)onError: (IFlySpeechError *) error{NSLog(@"errorCode:%d",[error errorCode]);NSString *resultStr = [_resultARR componentsJoinedByString:@" "];UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"" message:resultStr delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:nil];[alertV show];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

效果图:

最后,为了提高语音识别精确读,在程序启动时加入以下代码

#pragma mark----------语音转文字时需要精确辨别的词----------
- (void)setWordsIUsed{//用户词表
#define USERWORDS   @"{\"userword\":[{\"name\":\"iflytek\",\"words\":[\"指动生活\",\"指动\",\"生活\",\"温馨提示\",\"温馨\",\"提示\",\"家园活动\",\"家园\",\"活动\",\"智能门禁\",\"智能\",\"门禁\",\"投诉报修\",\"投诉\",\"报修\",\"物业缴费\",\"交费\",\"缴费\"]}]}"//创建上传对象_uploader = [[IFlyDataUploader alloc] init];IFlyUserWords *iFlyUserWords = [[IFlyUserWords alloc] initWithJson:USERWORDS ];//设置上传参数[_uploader setParameter:@"uup" forKey:@"sub"];[_uploader setParameter:@"userword" forKey:@"dtt"];//启动上传(请注意name参数的不同)[_uploader uploadDataWithCompletionHandler:^(NSString * grammerID, IFlySpeechError *error){//}name: @"userwords" data:[iFlyUserWords toString]];}

百度云盘:讯飞语音使用

iOS 讯飞语音的使用相关推荐

  1. iOS: 讯飞语音的使用

    一.介绍: 讯飞语音做的相当不错,容错率达到90%多,如果需要做语音方面的功能,它绝对是一个不错的选择.讯飞语音的功能很多:语音听写.语音识别.语音合成等,但我们最常用的还是语音听写.讯飞语音中包含界 ...

  2. iOS 讯飞语音之语音听写 录音的实现

    引入库 第一步 获取appid appid是第三方应用集成讯飞开放平台SDK的身份标识,SDK静态库和appid是绑定的,每款应用必须保持唯一,否则会出现10407错误码.appid在开放平台申请应用 ...

  3. html5语音听写流式,iOS 讯飞语音听写(流式版)

    最近项目中用到了讯飞的语音识别,然后稍微看了一下,里面有几个值得注意的点,记录一下,先说语音听写(流式版),实时语音转写后期会附上 ,文末有 demo //语音听写(流式版) 语音听写流式版其实没设么 ...

  4. iOS使用讯飞语音-语音识别(最新版)

    引言 去年在一家公司实习,接触了一点人工智能.既然接触人工智能,那么语音识别.语音合成.人脸识别等等都是必不可少的. 本来已经是离开iOS开发这条路了,还剩下毕业设计要做,所以这段时间最后一次回来接触 ...

  5. iOS接入讯飞语音,声音没有bug

    最近在做直播间项目,接入了讯飞语音的听写功能,然而识别出语音并转成文字之后,直播间播放的视频内容的声音没有了,参考了某大牛的解决方法 需要写一个分类 图如下;ok解决 更多问题,欢迎加群讨论:qq群 ...

  6. Android——讯飞语音唤醒简介及实现

    前段时间写了一个关于百度语音唤醒文章,最近有做了一个讯飞语音的应用,在这里把学习的资料整理一下. 唤醒的整个过程如下: (图源自讯飞官网) 讯飞的语音唤醒功能实现起来挺方便的(相比百度,百度语音And ...

  7. 为知笔记,讯飞语音平台上的故事

    为知笔记,移动互联时代资料管理的精品云服务 无论是团队还是个人,我们一直期望有一款云服务产品,不仅能即时保存.随时查找各种信息,还能方便地回顾和整理,与伙伴进行传递.共同撰写与修改,甚至基于这些资料展 ...

  8. Android自带语音播报+讯飞语音播报封装(直接用)

    一.Android自带的语音播报 1.查看是否支持中文,在测试的设备中打开'设置' -->找到 '语言和输入法'-->查看语音选项,是否支持中文,默认仅支持英文. 使用如下: public ...

  9. (原创)用讯飞语音实现人机交互的功能

    目前在做一款车载的项目,其中有一个需求是在开车的时候实现人与手机的对话,全过程不需要用手,只用语音操控. 这个就类似于人与机器人的对话,机器人在后台一直待命,用户说话 机器人做出对应的反映. 但由于用 ...

最新文章

  1. (已解决)pycharm调试报错-UnicodeDecodeError:‘utf-8‘ codec can‘t decode byte 0xe8 in position 1023
  2. 《Linux设备驱动程序》学习2—高级字符设备驱动ioctl
  3. Linux内核入门-如何获取Linux内核源代码、生成配置内核
  4. [导入]16:50论坛终于恢复了
  5. 项目清理和删除svn信息(转)
  6. Android开发检测网络是否连接
  7. 百度 AI Studio 车牌识别
  8. java和C程序员工资待遇差别大吗
  9. 免费ftp空间的文件传输
  10. Linux系统下安装redis
  11. 遇到问题--python---ImportError: Missing optional dependency ‘xlrd‘. Install xlrd >= 1.0.0 for Excel supp
  12. 分享一些前端开发人员必备的工具,脚本和资源
  13. ICMPv6与NDP
  14. 20170627总结
  15. Mac 键盘符号说明
  16. 计算数据的平均值、方差和标准差
  17. Java新手教程从入门到放弃从入门到精通(3)
  18. c++01(数据类型 运算符 选择结构)
  19. 五分钟了解机智云自助接入流程
  20. Linux进程KILL--Quit,INT,HUP,QUIT,和TERM、PIPE的解释

热门文章

  1. Redis一篇从入门到实战
  2. Win10搭建星际争霸2SC2LE环境
  3. TOMCAT 汤姆猫的 安装部署 优化
  4. 2009全国各省市本三学校|三本院校|本三招生|独立学院名录大全
  5. 英雄联盟Mac中文版(lol mac美服汉化版)
  6. 解决VMware Workstation的虚拟化与win10 自带的Hyper-V服务冲突的问题
  7. 软件测试技术之如何编写测试用例(5)
  8. 国产ARM核心板推荐
  9. 手机备忘录扫描功能怎么用
  10. 甲骨文客户端配置 ORALCE PLSQL 远程 连接