科大讯飞语音iOS版

转自:http://www.devstore.cn/evaluation/testInfo/107-127.html

集成过程

客户端集成

如何快速集成科大讯飞语音云:

注册科大讯飞语音云帐号=》创建应用=》下载SDK=》集成开发=》测试应用=》发布应用

1.注册科大讯飞语音云账号

科大讯飞语音云的注册地址:

http://member.voicecloud.cn/index.php/default/register?referer=

如果有新浪、人人、QQ、CSDN账号也可以直接授权登录

其他方式登陆

2.创建新应用

进入语音云开放平台首页,点击右上方的我的语音云,创建应用:

创建新应用

点击提交,进入应用详情页,这时候没有开通任何服务。

语音云测试

点击立即开通,选择语音基础能力,确定。

选择要开通的业务

3.下载IOS SDK

紧跟上一步,开通语音基础能力后,进入语音基础能力页面:

SDK下载

点击SDK下载选择带语音界面的,再次点击下载即可下载到最新SDK包。解压下载的文件,lib文件夹中得ifyMSC.framework就是我们需要的语音云开发工具。

4.集成开发

    1)新建工程

新建工程

    2)添加静态库

将开发工具包中lib目录下地iflyMSC.framework添加到工程中去。再添加如下图所示的库:

添加静态库

    3)导入头文件

在.pch文件导入如下头文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <iflyMSC/IFlyRecognizerViewDelegate.h>
#import <iflyMSC/IFlyRecognizerView.h>
  
#import <iflyMSC/IFlySpeechRecognizerDelegate.h>
#import <iflyMSC/IFlySpeechRecognizer.h>
  
#import <iflyMSC/IFlySpeechUnderstander.h>
  
#import <iflyMSC/IFlySpeechSynthesizerDelegate.h>
#import <iflyMSC/IFlySpeechSynthesizer.h>
  
#import <iflyMSC/IFlyContact.h>
#import <iflyMSC/IFlyDataUploader.h>
#import <iflyMSC/IFlyUserWords.h>
#import <iflyMSC/IFlySpeechConstant.h>
#import <iflyMSC/IFlySpeechUtility.h>

    4)初始化SDK

在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加如下代码:

?
1
2
3
NSString *appid = @ "53be3a59" ;
     NSString *initString = [NSString stringWithFormat:@ "appid=%@" ,appid];
[IFlySpeechUtility createUtility:initString];

请自行根据自己应用的APPID修改appid字符串内容。

    5)语音转写,带界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//初始化语音识别控件
     self.iflyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
     self.iflyRecognizerView.delegate = self;
     [self.iflyRecognizerView setParameter: @ "iat"  forKey:[IFlySpeechConstant IFLY_DOMAIN]];
     //asr_audio_path保存录音文件名,默认目录是documents
     [self.iflyRecognizerView setParameter: @ "asrview.pcm"  forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
         //设置返回的数据格式为默认plain
     [self.iflyRecognizerView setParameter:@ "plain"  forKey:[IFlySpeechConstant RESULT_TYPE]];
- ( void )startListenning:(id)sender{
     [self.iflyRecognizerView start];
NSLog(@ "开始识别" );}
//返回数据处理
- ( void )onResult:(NSArray *)resultArray isLast:( BOOL )isLast
{
     NSMutableString *result = [NSMutableString  new ];
     NSDictionary *dic = [resultArray objectAtIndex:0];
     NSLog(@ "DIC:%@" ,dic);
     for  (NSString *key in dic) {
         [result appendFormat:@ "%@" ,key];
     }
     self.textView.text = [NSString stringWithFormat:@ "%@%@" ,self.textView.text,result];}
  
- ( void )onError:(IFlySpeechError *)error
{
     NSLog(@ "RecognizerView error : %@" ,error.errorDesc);}

    6)上传联系人

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//创建上传实例
self.uploader = [IFlyDataUploader  new ];
//获取联系人
IFlyContact *iflyContact = [IFlyContact  new ];
NSString *contact = [iflyContact contact];
  
[self.uploader setParameter:@ "uup"  forKey:@ "subject" ];
[self.uploader setParameter:@ "contact"  forKey:@ "dtt" ];
[self.uploader uploadDataWithCompletionHandler:^(NSString *result, IFlySpeechError *error) {
         NSLog(@ "uploader contact error: %d" ,[error errorCode]);
         if  (![error errorCode]) {
             [self.popUpView setText: @ "上传成功" ];
             [self.view addSubview:self.popUpView];
         }
         else  {
             [self.popUpView setText: @ "上传失败" ];
             [self.view addSubview:self.popUpView];
         }
     } name:@ "contact"  data: contact];

    7)上传用户词表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[self.uploader setParameter:@ "iat"  forKey:@ "subject" ];
[self.uploader setParameter:@ "userword"  forKey:@ "dtt" ];}
IFlyUserWords *iflyUserWords = [[IFlyUserWords alloc] initWithJson:@ "{\"userword\":[{\"name\":\"iflytek\",\"words\":[\"DevStore\",\"Steven\",\"清蒸鲈鱼\",\"挪威三文鱼\",\"黄埔军校\",\"横沙牌坊\",\"科大讯飞\"]}]}"  ];
[self.uploader uploadDataWithCompletionHandler:^(NSString *result, IFlySpeechError *error) {
         NSLog(@ "upload userwords error: %d" ,[error errorCode]);
         if  (![error errorCode]) {
             [self.popUpView setText: @ "上传成功" ];
             [self.view addSubview:self.popUpView];
         }
         else  {
             [self.popUpView setText: @ "上传失败" ];
             [self.view addSubview:self.popUpView];
         }
     } name:@ "userwords"  data:[iflyUserWords toString]];

    8)语义识别

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
self.iflySpeechUnderstander = [IFlySpeechUnderstander sharedInstance];
self.iflySpeechUnderstander.delegate = self;
[self.iflySpeechUnderstander startListening];
- ( void )onResults:(NSArray *)results isLast:( BOOL )isLast{
     NSMutableString *resultString = [[NSMutableString alloc] init];
     NSDictionary *dic = results [0];
     for  (NSString *key in dic) {
         [resultString appendFormat:@ "%@" ,key];
     }
     NSLog(@ "听写结果:%@" ,resultString);
}
- ( void )onError:(IFlySpeechError *)errorCode{
     NSString *text ;
if  (errorCode.errorCode ==0 ) {
         if  (result.length==0) {
             text = @ "无识别结果" ;
         }
         else
         {
             text = @ "识别成功" ;
         }
     }
     else
     {
         text = [NSString stringWithFormat:@ "发生错误:%d %@" ,errorCode.errorCode,errorCode.errorDesc];
}
NSLog(@ "%@" ,text);
}

    9)语音合成

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//头文件定义
@interface SpeechSynViewController : UIViewController <IFlySpeechSynthesizerDelegate>
@property (nonatomic,strong)IFlySpeechSynthesizer *iflySpeechSyn;    
//.m文件定义
#define SPEAKING @"你是我的小呀小苹果儿,怎么爱你都不嫌多。\
红红的小脸儿温暖我的心窝,点亮我生命的火,火火火火。\
你是我的小呀小苹果儿,就像天边最美的云朵。\
春天又来到了花开满山坡,种下希望就会收获。"
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if  (self) {
         //创建合成对象
         self.iflySpeechSyn = [IFlySpeechSynthesizer sharedInstance];
         self.iflySpeechSyn.delegate = self;
         //设置语音合成的参数
         //语速,取值范围 0~100
         [self.iflySpeechSyn setParameter:@ "50"  forKey:[IFlySpeechConstant SPEED]];
         //音量;取值范围 0~100
         [self.iflySpeechSyn setParameter:@ "50"  forKey:[IFlySpeechConstant VOLUME]];
         //发音人,默认为”xiaoyan”;可以设置个性化发音人列表
         [self.iflySpeechSyn setParameter:@ "xiaoyan"  forKey:[IFlySpeechConstant VOICE_NAME]];
         //音频采样率,目前支持的采样率有 16000 和 8000
         [self.iflySpeechSyn setParameter:@ "8000"  forKey:[IFlySpeechConstant SAMPLE_RATE]];
         //asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是 documents
         [self.iflySpeechSyn setParameter:@ "tts.pcm"  forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
}
     return  self;
}
//开始播放
- ( void ) onStart:(id) sender
{
[self.iflySpeechSyn startSpeaking:SPEAKING];
}
//合成结束,此代理必须要实现
- ( void ) onCompleted:(IFlySpeechError *)error
{
     NSString *text;
     if  (self.isCanceled)
     {
         text = @ "合成已取消" ;
     }
     else  if (error.errorCode == 0)
     {
         text = @ "合成结束" ;
     }
     else
     {
         text = [NSString stringWithFormat:@ "发生错误:%d %@" ,error.errorCode,error.errorDesc];
         NSLog(@ "%@" ,text);
     }
}

主要功能

语音识别:

语音识别

语义识别:

语义识别

语音合成(嘿嘿,你们听不到):

语音合成

功能特色

获取通讯录中的联系人:

获取通讯录中的联系人

获取用户词表:

获取用户词表

上传数据:

上传数据

个性发音人:

个性发音人

DEMO展示

部分测试DEMO如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if  (self) {
         //初始化语音识别控件
         self.iflyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
         self.iflyRecognizerView.delegate = self;
         [self.iflyRecognizerView setParameter: @ "iat"  forKey:[IFlySpeechConstant IFLY_DOMAIN]];
         //asr_audio_path保存录音文件名,默认目录是documents
         [self.iflyRecognizerView setParameter: @ "asrview.pcm"  forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
         //设置返回的数据格式为默认plain
         [self.iflyRecognizerView setParameter:@ "plain"  forKey:[IFlySpeechConstant RESULT_TYPE]];
     }
     return  self;
}
  
- ( void )viewDidLoad
{
     [super viewDidLoad];
     UIPlaceHolderTextView *resultView = [[UIPlaceHolderTextView alloc]initWithFrame:CGRectMake(Margin*2, Margin*2, self.view.frame.size.width - Margin*4, 340)];
     resultView.layer.cornerRadius = 8;
     resultView.layer.borderWidth = 1;
     [self.view addSubview:resultView];
     resultView.font = [UIFont systemFontOfSize:17.0f];
     resultView.placeholder = @ "识别结果" ;
     resultView.editable = NO;
     self.textView = resultView;
     
     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
     button.frame = CGRectMake(Margin*2, resultView.frame.origin.y+resultView.frame.size.height+10, self.view.frame.size.width-Margin*4, 50);
     [button setTitle:@ "开始识别"  forState:UIControlStateNormal];
     [button addTarget:self action:@selector(startListenning:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:button];
     
     self.popView = [[PoppupView alloc] initWithFrame:CGRectMake(100, 300, 0, 0)];
     self.popView.parentView = self.view;
  
}
  
- ( void )startListenning:(id)sender{
     [self.iflyRecognizerView start];
     NSLog(@ "开始识别" );
}
  
- ( void )didReceiveMemoryWarning
{
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
//返回数据处理
- ( void )onResult:(NSArray *)resultArray isLast:( BOOL )isLast
{
     NSMutableString *result = [NSMutableString  new ];
     NSDictionary *dic = [resultArray objectAtIndex:0];
     NSLog(@ "DIC:%@" ,dic);
     for  (NSString *key in dic) {
         [result appendFormat:@ "%@" ,key];
     }
     self.textView.text = [NSString stringWithFormat:@ "%@%@" ,self.textView.text,result];
}
  
- ( void )onError:(IFlySpeechError *)error
{
NSLog(@ "RecognizerView error : <a href=" mailto:%@\ " ,error.errordesc" = "" >%@",error.errorDesc);}

更多测试DEMO可以下载评测DEMO包查看

测试日志

初始化:

初始化

语音识别:

语音识别

语义识别:

语义识别

遇到问题

SDK集成后编译报错,找不到SDK的头文件:

1.确认Framework Search Path路径,找到Project=>Buildsetting=>Search Path=> Framework Search Paths,查对应的目录下是否存在SDK。

2.英文识别:[iflySpeechRecognizer setParameter:@"language" value:@"en_us"];

上手难易

科大讯飞语音云集成简单,开发者只需要熟悉面向对象的编写方式,了解程序的基本架构,就可以轻松集成自己的项目。

开发文档

科大讯飞云语音提供iOS平台的SDK下载,在下载的SDK压缩包里包括了新手集成pdf文件和一个关于API的.doc文件,doc文件需要自行导入到Xcode中去,以观看API文档。

开发文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值