使用讯飞语音实现语音识别,朗读文字
2016-01-02 20:47
417 查看
一.走近讯飞(iFly)
语音技术实现了人机语音交互,使人与机器之间沟通变得像人与人沟通一样简单。语音技术主要包括语音合成和语音识别两项关键技术。让机器说话,用的是语音合成技术;让机器听懂人说话,用的是语音识别技术。此外,语音技术还包括语音编码、音色转换、口语评测、语音消噪和增强等技术,有着广阔应用空间。早期的语音识别技术让人啼笑皆非, 就连Siri刚出道时, 也是漏洞百出. 但是讯飞通过多年的不懈努力, 最近发展迅速, 这也是技术型项目前期技术积累的必然结果.
百度也推出了自己的语音识别, 但是因为技术积累尚浅, 移植和测试体验尚不如讯飞 – 本条个人观点.
科大讯飞从开始的只做语音识别和语音合成, 到现在的
广告+统计+广场+人脸识别+声纹识别+推送, 可以看出它的野心–打造综合性平台, 同时又不放弃专营业务(并且拥有难以记忆的英文缩写和logo).
从使用讯飞的SDK过程中, 还是能感觉到诚意的, 很多设计很人性化, 免费提供了诸多测试和使用接口, 让人好感倍增, 这也是为啥我为其做了这么多广告.
二.搭建环境
登录开发者平台
注册用户并且登录
创建新应用
选择创建新应用:
file-list
这里可以比较随意填写, 但是注意平台别搞错.
file-list
应用创建好之后, 请记录下讯飞为该APP生成的Appid: 56678310 (每个人都不一样哦)
为新应添加服务
新创建的应用可以在”我的应用”中查看, 开始的时候, 这个应用没有使用任何SDK, 我们需要向讯飞注册一下我们的app都需要哪些服务.
file-list
点击”开通更多服务”, 选择语言听写和在线语音合成两个SDK, 第一个开发语义是自己添加上的.
file-list
下载相应SDK
进入下载SDK界面, 您可以通过诸多位置进入到这里, 可能与截图不符, 但没有问题.
file-list
这里选择”组合服务SDK下载”, 勾选图中前两个.
file-list
选择平台
file-list
最后选择刚才创建的引用, 之后点击下载.
file-list
新建xcode(singleView)工程, 将下载好的文件夹中lib下的iflyMac导入(拖入)工程
添加引用库
file-list
三.代码+++++
在storyBoard的viewController中拖入几个控件, 一个UILable用来显示语音翻译后的文字, 两个UIbutton用来触发”带界面的实时翻译”和”不带界面的实时翻译”. 并为他们拖出属性和响应方法.如图:
file-list
appdelegate.m中, 添加如下代码(注册):
AppDelegate.m 的 didFinishLaunchingWithOptions中:
1 2 3 4 5 6 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 根据appid登录到讯飞的服务器, 过程需要身份验证 , 56678310 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"你的appid, 别用我的"]; [IFlySpeechUtility createUtility:initString]; return YES; } |
1 2 3 4 5 67 | #import "ViewController.h" #import <iflyMSC/iflyMSC.h> // 这个头文件是做什么的呢? #import "ISRDataHelper.h" // 还有这个. #import "IATConfig.h" @interface ViewController ()<IFlyRecognizerViewDelegate, IFlySpeechRecognizerDelegate> // 翻译好的Text会展示在这个label上. @property (weak, nonatomic) IBOutlet UILabel *textView; /*! * 语音识别控件 * 录音时触摸控件结束录音,开始识别(相当于旧版的停止);触摸其他位置,取消录音,结束会话(取消) * 出错时触摸控件,重新开启会话(相当于旧版的再说一次);触摸其他位置,取消录音,结束会话(取消) * */ @property (nonatomic,strong)IFlyRecognizerView * iflyRecognizerView; /*! * 语音识别类 * 此类现在设计为单例,你在使用中只需要创建此对象,不能调用release/dealloc函数去释放此对象。所有关于语音识别的操作都在此类中。 */ @property (nonatomic, strong)IFlySpeechRecognizer * iFlySpeechRecognizer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 语音识别视图空间及配置. [self initRecognizerView]; // 语音识别类的初始化及配置. [self initSpeechRecognizer]; } // !!!:语音识别视图空间及配置--方法. -(void)initRecognizerView{ _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center]; _iflyRecognizerView.delegate = self; [_iflyRecognizerView setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]]; //asr_audio_path保存录音文件名,如不再需要,设置value为nil表示取消,默认目录是documents [_iflyRecognizerView setParameter:@"asrview.pcm " forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; } // !!!:语音识别类的初始化及配置--方法. -(void)initSpeechRecognizer{ //单例模式,无UI的实例 if (_iFlySpeechRecognizer == nil) { _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance]; [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]]; //设置听写模式 [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]]; } _iFlySpeechRecognizer.delegate = self; if (_iFlySpeechRecognizer != nil) { IATConfig *instance = [IATConfig sharedInstance]; //设置最长录音时间 [_iFlySpeechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]]; //设置后端点 [_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]]; //设置前端点 [_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]]; //网络等待时间 [_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]]; //设置采样率,推荐使用16K [_iFlySpeechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]]; if ([instance.language isEqualToString:[IATConfig chinese]]) { //设置语言 [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]]; //设置方言 [_iFlySpeechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]]; }else if ([instance.language isEqualToString:[IATConfig english]]) { [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]]; } //设置是否返回标点符号 [_iFlySpeechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // !!!: push界面的识别,点击事件 - (IBAction)voiceToText:(id)sender { [self.iflyRecognizerView start]; } // !!!: 触发语音识别类的点击事件 - (IBAction)voiceToTextWithoutUI:(id)sender { self.textView.text = @""; // 这个需要手动停止翻译. [_iFlySpeechRecognizer cancel]; //设置音频来源为麦克风 [_iFlySpeechRecognizer setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"]; //设置听写结果格式为json [_iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]]; //保存录音文件,保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下 [_iFlySpeechRecognizer setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; [_iFlySpeechRecognizer setDelegate:self]; [_iFlySpeechRecognizer startListening]; } // !!!:实现代理方法 // !!!:注意有没有s, 语音识别的结果回调(带界面的那个) -(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast { NSMutableString *result = [[NSMutableString alloc] init]; NSDictionary *dic = [resultArray objectAtIndex:0]; for (NSString *key in dic) { [result appendFormat:@"%@",key]; } // 注意: 语音识别回调返回结果是一个json格式字符串, 解析起来比较麻烦, 但是我们只需要其中的字符串部分, 这个过程讯飞也觉得麻烦, 就推出了一个工具类, 能将这个josn解析最终字符串返回. 这也是前面导入ISRDataHelper.h的作用. NSString * resu = [ISRDataHelper stringFromJson:result]; self.textView.text = [NSString stringWithFormat:@"%@%@",self.textView.text,resu]; } // !!!:解析失败代理方法 -(void)onError:(IFlySpeechError *)error { NSLog(@"解析失败了"); } // !!!:语音识别类的回调方法(不带界面的那个) - (void) onResults:(NSArray *) results isLast:(BOOL)isLast{ NSMutableString *result = [[NSMutableString alloc] init]; NSDictionary *dic = [results objectAtIndex:0]; for (NSString *key in dic) { [result appendFormat:@"%@",key]; } NSString * resu = [ISRDataHelper stringFromJson:result]; self.textView.text = [NSString stringWithFormat:@"%@%@", self.textView.text, resu]; } @end |
1 2 | #import "ISRDataHelper.h" #import "IATConfig.h" |
算了, 最后我把这个工程传到git上吧. 你们从哪里扒下来就好了.
四.语音合成
其实iOS自带语音合成, 我们不必使用讯飞也可以达到这样的效果, 下面的代码能让你的APP读出这些文字.Appdelegate.m中, 添加一个延展, 并且 didFinishLaunchingWithOptions 中添加如下代码:
1 2 3 4 5 67 | @interface AppDelegate () @property(nonatomic,strong)AVSpeechSynthesizer * speechSynthesizer; // 合成器 @property(nonatomic,strong)AVSpeechUtterance * speechUtterance; // 合成器所说的内容 @end - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init]; self.speechUtterance = [[AVSpeechUtterance alloc] initWithString:@"啪啪啪"]; [self.speechSynthesizer speakUtterance:self.speechUtterance]; return YES; } |
使用讯飞实现啪啪啪的功能
我们直接在上面的工程里添加吧.
首先在sb中的viewcontroller里, 再拖一个textfiled, 我们让讯飞朗读textfiled中的内容.
file-list
viewController.m, 这个将三个功能写到了同一个controller中.比较臃肿, 你们自己捋顺一下, 封装成类, 供以后使用.
1 2 3 4 5 67 | #import "ViewController.h" |
相关文章推荐
- Java Web 跨平台 - 使用Apache Axis2引擎发布基于Java语言的WebService并跨平台调用实例
- Burp Suite抓包、截包和改包
- python基础-面向对象
- 关于margin-top失效的解决办法
- Hibernate中常见问题 No row with the given identifier exists问题的原因及解决
- 使用存储过程查询并按每页10条记录分页显示图书借阅纪录
- 编译安装httpd-2.4
- 对Xcode菜单选项的详细探索(来自董铂然的微博http://www.cnblogs.com/dsxniubility/p/4983614.html)
- 关于padding
- 哈夫曼树及哈夫曼编码 C++
- Html知识点
- N皇后问题
- hadoop-1.2.1 Eclipse plugin
- linux下makefile配置文件及make工具的使用
- sublime text3注册码
- 【Android】SDK Manager 设置代理
- 图片加载框架Glide使用教程
- ubuntu linux 14.04 dsl 不稳定总结
- 【c#】树节点控件
- 实现ViewPager新手引导页的指示点跟随移动的效果