您的位置:首页 > 移动开发 > IOS开发

iOS开发(第三方使用)——讯飞语音SDK接入

2016-09-18 12:45 691 查看
去到讯飞开放平台创建应用并添加服务

下载SDK,下载时需要选上项目的,必须选上相应的项目,不能用项目1下载的SDK和项目2的app ID结合使用(估计是讯飞绑定了,所以步骤1和步骤2也不能颠倒)

拷贝下载的SDK中的iflyMSC.framework到桌面,然后拖到工程去

选择刚刚拖进的iflyMSC.framework,show in finder,然后按照下图操作,双击右边部分,会弹出一个大框,把iflyMSC.framework所在的文件夹拖到大框里。



添加类库,如下图



在AppDelegate.m导入头文件iflyMSC/IFlyMSC.h 并添加一下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//讯飞
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"你的app ID"];
[IFlySpeechUtility createUtility:initString];

return YES;
}

/*
讯飞
*/
1. (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
[[IFlySpeechUtility getUtility] handleOpenURL:url];
return YES;
}

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}


7.在下载的SDK里面找到下图的几个文件,并拖到工程



8.在你需要用到的控制器里面导入头文件,并设置代理IFlySpeechRecognizerDelegate

#import "iflyMSC/IFlySpeechRecognizerDelegate.h"
#import "iflyMSC/IFlySpeechRecognizer.h"
#import "iflyMSC/IFlyMSC.h"
#import "IATConfig.h"
#import "ISRDataHelper.h"


9.声明实例IFlySpeechRecognizer *_iFlySpeechRecognizer;在语音设别按钮添加以下代码

if(_iFlySpeechRecognizer == nil)
{
[self initRecognizer];
}
[_iFlySpeechRecognizer cancel];

//设置音频来源为麦克风
[_iFlySpeechRecognizer setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];
//设置听写结果格式为json
[_iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]];
[_iFlySpeechRecognizer setDelegate:self];

BOOL ret = [_iFlySpeechRecognizer startListening];

if (ret) {
NSLog(@"start");
}else{
NSLog(@"error");
}


10.其中initRecognizer方法如下

-(void)initRecognizer
{
//单例模式,无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]];
}
//0无标点返回
[_iFlySpeechRecognizer setParameter:@"0" forKey:[IFlySpeechConstant ASR_PTT]];

}
}


11.代理方法

- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
NSMutableString *resultString = [[NSMutableString alloc] init];
NSDictionary *dic = results[0];
for (NSString *key in dic) {
[resultString appendFormat:@"%@",key];
}
NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];

NSLog(@"resultFromJson=%@",resultFromJson);
}
//识别会话错误返回
- (void)onError: (IFlySpeechError *) error
{
//error.errorCode =0 听写正确  other 听写出错
NSLog(@"code=%d",error.errorCode);
if(error.errorCode!=0){
//出错
}
}


iOS开发交流群:301058503
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  讯飞语音