科大讯飞语音识别

1.科大讯飞开放平台 

2.科大讯飞iOS - API开放平台

        那下面我们来看一下科大讯飞的开发步骤吧

第一步:申请账号ID

登陆到讯飞开放平台上,在用户菜单栏里创建应用,这里的登陆也可以采用第三方的方式,在创建应用的界面填写相关的信息即可,然后就会有一个SDK的下载链接,如果没有直接去SDK选项下下载即可。

第二步:导入讯飞SDK框架

下载下来SDK解压后有三个文件夹:doc文件夹:不用多说肯定是开发文档;重要的是接下来的那两个文件夹:一个是lib文件夹:存放科大讯飞SDK类库,这就是我们要导入的SDK;Sample:iOS的科大讯飞demo演示工程。

下面我们来创建一个工程,将lib文件夹下的“iflyMSC.framework”拷贝到工程目录,然后在工程中添加依赖库,如下图所示:

第三步:开始进行语音识别了

语音识别分两种,分别用在不同场合,一个是界面提示的语音识别,一个是无界面提示的语音识别,这里以有界面提示的语音识别为例先进性讲解。

3.1导入头文件

 1 #import "iflyMSC/IFlySpeechUtility.h" 

3.2登陆科大讯飞语音平台

在使用讯飞的语音解析之前,需要进行用户身份验证,即登陆讯飞服务器,即讯飞服务器需要根据你当前用户的APPID才能同意你登陆。代码如下:

 

1     //第二步:登陆科大讯飞语音平台
2     NSString *appID = [NSString stringWithFormat:@"appid=%@",@"570f0a8b"];
3     [IFlySpeechUtility createUtility:appID];

 

3.3创建有界面提示语音识别对象

创建一个讯飞语音识别对象,可以对他进行一系列的调用
 1 #import "FirstViewController.h"
 2 
 3 //第一步:引入库文件
 4 //科大讯飞语音识别功能回调方法的接口文件
 5 #import <iflyMSC/IFlyRecognizerViewDelegate.h>
 6 //科大讯飞语音识别功能的声音识别视图
 7 #import <iflyMSC/IFlyRecognizerView.h>
 8 //科大讯飞语音识别功能中定义的常量
 9 #import <iflyMSC/IFlySpeechConstant.h>
10 
11 ///遵循代理协议
12 @interface FirstViewController ()<IFlyRecognizerViewDelegate>
13 
14 ///语音识别对象
15 @property (nonatomic,strong)IFlyRecognizerView *iflyRecognizerView;
16 
17 ///接收相关结果的字符串
18 @property (nonatomic,strong)NSMutableString *result;
19 
20 ///展示识别内容的textView
21 @property (weak, nonatomic) IBOutlet UITextView *showContentTextView;
22 
23 
24 
25 @end
26 
27 @implementation FirstViewController
28 
29 - (void)viewDidLoad {
30     [super viewDidLoad];
31     // Do any additional setup after loading the view.
32     
33     
34     //创建声音识别视图对象,初始化声音识别控件
35     self.iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
36     //delegate需要设置,确保delegate回调可以正常返回
37     self.iflyRecognizerView.delegate = self;
38 
39 }
40 
41 #pragma mark - 开始识别
42 - (IBAction)beginRecognise:(id)sender {
43     
44     [self startListenning];
45     
46 }
47 
48 - (void)startListenning
49 {
50     //设置语音识别结果应用为普通文本领域
51     [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
52     //设置前端点检测时间为6000ms
53     [self.iflyRecognizerView setParameter: @"6000" forKey:[IFlySpeechConstant VAD_BOS]];
54     //设置后端点检测时间为700ms
55     [self.iflyRecognizerView setParameter: @"700" forKey:[IFlySpeechConstant VAD_EOS]];
56     //设置采样率为8000
57     [self.iflyRecognizerView setParameter: @"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
58     //设置为返回结果中包含标点符号
59     [self.iflyRecognizerView setParameter: @"1" forKey:[IFlySpeechConstant ASR_PTT]];
60     //设置语音识别完成后数据的返回数据结构类型xml
61     [self.iflyRecognizerView setParameter: @"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
62     //设置在Documents文件夹下缓存的文件名为temp.asr
63     [self.iflyRecognizerView setParameter: @"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
64     //设置自定义的参数
65     [self.iflyRecognizerView setParameter: @"custom" forKey:[IFlySpeechConstant PARAMS]];
66     
67     [self.iflyRecognizerView start];
68     
69 }
70 
71 #pragma mark - 代理方法
72 //成功
73 - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
74     
75     
76     self.result = [[NSMutableString alloc] init];
77     NSDictionary *dic = [resultArray objectAtIndex:0];
78     
79     for (NSString *key in dic)
80     {
81         [self.result appendFormat:@"%@",key];
82     }
83     NSLog(@"%@---------",_result);
84     
85     //自定义控件显示内容
86     self.showContentTextView.text = [NSString stringWithFormat:@"%@%@",self.showContentTextView.text,self.result];
87 }
88 
89 //失败
90 - (void)onError:(IFlySpeechError *)error{
91    
92     NSLog(@"%@",error);
93 }

4.1文字识别的回调方法接口

 1 #import "SecondViewController.h"
 2 
 3 //第一步:引入头文件
 4 //文字识别的回调方法接口
 5 #import <iflyMSC/IFlySpeechSynthesizerDelegate.h>
 6 //文字识别对象
 7 #import <iflyMSC/IFlySpeechSynthesizer.h>
 8 //科大讯飞语音框架定义的常量
 9 #import <iflyMSC/IFlySpeechConstant.h>
10 
11 @interface SecondViewController ()<IFlySpeechSynthesizerDelegate>
12 
13 //文字识别对象
14 @property (strong, nonatomic) IFlySpeechSynthesizer *synthesizer;
15 
16 
17 
18 ///输入内容的文本框
19 @property (weak, nonatomic) IBOutlet UITextView *inputContentTextView;
20 
21 @end
22 
23 @implementation SecondViewController
24 
25 - (void)viewDidLoad {
26     [super viewDidLoad];
27     // Do any additional setup after loading the view.
28     
29     //创建文字识别对象
30     self.synthesizer = [IFlySpeechSynthesizer sharedInstance];
31     
32     //指定文字识别对象的代理对象
33     self.synthesizer.delegate = self;
34     
35     //设置文字识别对象的关键属性
36     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
37     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
38     [self.synthesizer setParameter:@"XIAOYAN" forKey:[IFlySpeechConstant VOICE_NAME]];
39     [self.synthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
40     [self.synthesizer setParameter:@"temp.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
41     [self.synthesizer setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
42     
43 }
44 
45 #pragma mark - 识别相关的内容
46 - (IBAction)beginRecognise:(id)sender {
47    
48     [self.synthesizer startSpeaking:@"最后一节课了,大家以后要加油"];
49     
50 }
51 
52 #pragma mark - 代理方法
53 - (void)onCompleted:(IFlySpeechError *)error{
54     
55     NSLog(@"%@",error);
56 }

 

 

posted @ 2016-06-03 17:21  雷坤  阅读(2985)  评论(0编辑  收藏  举报