首页 > 代码库 > (转)IOS App中揉合讯飞SDK功能详细
(转)IOS App中揉合讯飞SDK功能详细
转至:http://www.sufeinet.com/thread-2488-1-1.html
如何使用讯飞的语音SDK加入到你的iPhone应用中吧!
1.首先请先到讯飞官方网站的开发者专区(http://open.voicecloud.cn/developer.php)。如果你还没有注册,那么需要先注册一个开发者账号的。点击网站顶部的“免费注册”即可,使用你的自己的手机号码注册一个账号。
2.仅仅注册了一个开发者账号还可以使用SDK,因为还需要申请Appid。登陆之后到自己的个人中心创建一个应用填写好相关信息,然后等待审核通过,因为只有审核通过之后,你才可以下载讯飞语音的SDK。如下:
<ignore_js_op>
3.如果你的应用已经审核通过,点击SDK下载,并且选择对应平台的SDK,比如现在选择iphone平台SDK下载。
4.下载好了之后,你会看到一个压缩包,解压出来有四个文件,
<ignore_js_op>
其中sample就是讯飞提供的DEMO了,打开项目,你会看到这样的目录结构:
<ignore_js_op>
直接运行,并没有错误,只有几个警告,运行界面如下:
<ignore_js_op>
< ignore_js_op>
5.那么怎么在自己的项目中使用呢?其实还是非常简单的。讯飞也有提供相关的开发文档,还是比较详细。在开发者专区和下载专区都有文档可供下载。
6.新建一个iPhone 项目吧。新建好了之后,需要先导入需要的lib,如下:
<ignore_js_op>
其中需要说明的是,iflyMSC.framework这个lib就在你下载的SDK解压文件下的lib文件夹下。在添加lib的界面选择Add Others...,并且选择你下载的lib。点击Open,就能正确添加。
<ignore_js_op>
7.一些函数和配置的说明:
在你需要使用SDK的头文件中导入文件:
- #import "iFlyMSC/IFlyRecognizeControl.h"
- #import "iFlyMSC/IFlysynthesizerControl.h"
创建一个识别控件或者合成控件:
- IFlyRecognizeControl *_iFlyRecognizeController; //識別控件
- IFlySynthesizerControl *_iFlySynthesizerControl; //合成控件
在实现文件初始化控件:
- //初始化語音識別控件
- _iFlyRecognizeController = [[IFlyRecognizeControl alloc] initWithOrigin:CGPointMake(20, 70) initParam:initPara];
- [self.view addSubview:_iFlyRecognizeController];
- //Configure
- [_iFlyRecognizeController setEngine:@"sms" engineParam:nil grammarID:nil];
- [_iFlyRecognizeController setSampleRate:16000];
- [_iFlyRecognizeController setDelegate:self];
- [_iFlyRecognizeController setShowLog:NO];
- //註冊unActive事件
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationWillResignActiveNotification object:nil];
- //初始化語音合成控件
- _iFlySynthesizerControl = [[IFlySynthesizerControl alloc] initWithOrigin:CGPointMake(20, 70) initParam:initPara];
- //Configure
- [_iFlySynthesizerControl setDelegate:self];
- [_iFlySynthesizerControl setVoiceName:@"vixm"]; //發音人(中英文粵語)
- [self.view addSubview:_iFlySynthesizerControl];
- //show UI
- [_iFlySynthesizerControl setShowUI:YES];
- //show log
- [_iFlySynthesizerControl setShowLog:NO];
- //註冊unActive事件
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignactiveOfSynthesizer) name:UIApplicationWillResignActiveNotification object:nil];
一些回调函数和方法:
- //識別結束回調函數-會話過程結束調用此函數
- - (void)onRecognizeEnd:(IFlyRecognizeControl *)iFlyRecognizeControl theError:(int)error
- {
- [_recognizeButton setEnabled:YES];
- [_synthesizerButton setEnabled:YES];
- NSLog(@"識別結束");
- NSLog(@"上傳流量:%d,下載流量:%d",[iFlyRecognizeControl getUpflow:FALSE],[iFlyRecognizeControl getDownflow:FALSE]);
- }
- //識別結果回調函數
- - (void)onResult:(IFlyRecognizeControl *)iFlyRecognizeControl theResult:(NSArray *)resultArray
- {
- NSString *strResult = [[resultArray objectAtIndex:0] objectForKey:@"NAME"];
- NSLog(@"識別的結果是:%@",strResult);
- }
- //開始語音識別
- - (void)onButtonRecognize
- {
- if ([_iFlyRecognizeController start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- }
- }
- //不支持後台模式則在unActive事件時執行cancel
- - (void)resignActive
- {
- [_iFlyRecognizeController cancel];
- }
- //合成回調函數,執行cancel函數整個會話結束調用此函數
- - (void)onSynthesizerEnd:(IFlySynthesizerControl *)iFlySynthesizerControl theError:(int)error
- {
- [_synthesizerButton setEnabled:YES];
- [_recognizeButton setEnabled:YES];
- NSLog(@"結束");
- NSLog(@"上傳流量:%d,下載流量:%d",[iFlySynthesizerControl getUpflow:FALSE],[iFlySynthesizerControl getDownflow:FALSE]);
- }
- //獲取播放器緩衝進度
- - (void)onSynthesizerBufferProgress:(float)bufferProgress
- {
- NSLog(@"當前緩衝進度:%f",bufferProgress);
- }
- //獲取播放器播放進度
- - (void)onSynthesizerPlayProgress:(float)playProgress
- {
- NSLog(@"當前播放進度:%f",playProgress);
- }
- //開始語音合成
- - (void)onButtonSynthesizer
- {
- [_iFlySynthesizerControl setText:@"哈哈,这仅仅是一个测试合成功能的例子而已,你不用太紧张会发生什么特别的事情。谢谢" params:nil];
- if ([_iFlySynthesizerControl start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- } else {
- NSLog(@"I‘m sorry,start error. ");
- }
- }
- //不支持後台模式則unActive事件時執行cancel
- - (void)resignactiveOfSynthesizer
- {
- NSLog(@"resignActive");
- [_iFlySynthesizerControl cancel];
- }
其中下列方法可以在自己需要开始使用语音功能的地方调用,比如:
- _recognizeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- _synthesizerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [_recognizeButton setFrame:CGRectMake(70, 100, 180, 60)];
- [_synthesizerButton setFrame:CGRectMake(70, 200, 180, 60)];
- [_recognizeButton setTitle:@"開始語音識別" forState:UIControlStateNormal];
- [_synthesizerButton setTitle:@"開始語音合成" forState:UIControlStateNormal];
- [_recognizeButton addTarget:self action:@selector(onButtonRecognize) forControlEvents:UIControlEventTouchDown];
- [_synthesizerButton addTarget:self action:@selector(onButtonSynthesizer) forControlEvents:UIControlEventTouchDown];
- [self.view addSubview:_recognizeButton];
- [self.view addSubview:_synthesizerButton];
- //開始語音識別
- - (void)onButtonRecognize
- {
- if ([_iFlyRecognizeController start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- }
- }
- //開始語音合成
- - (void)onButtonSynthesizer
- {
- [_iFlySynthesizerControl setText:@"这仅仅是一个测试合成功能的例子,你不用太紧张会发生什么" params:nil];
- if ([_iFlySynthesizerControl start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- } else {
- NSLog(@"I‘m sorry,start error. ");
- }
- }
8.到这里已经可以使用语音识别和合成功能了。如下:
<ignore_js_op>
< ignore_js_op>
< ignore_js_op>