科大讯飞语音识别

先到科大讯飞开放平台注册帐号,建立应用并下载SDK,解压SDK压缩包,将framework拖到工程中,以下图前端

而后还要导入一下类库:具体以下图后端

建立一个工程,咱们分红两部分,第一部分就是在FristViewController中,实现语音识别功能,第二部分就是在SecondViewController中,实现文字转换为语音缓存

首先在AppDelegate.m导入头文件数据结构

1 // 导入头文件
2 #import <iflyMSC/iflyMSC.h>

而后在AppDelegate.m中实现登陆科大讯飞语音平台app

1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2     // Override point for customization after application launch.
3     
4     // 第二步:登陆科大讯飞语音平台 【换成本身的讯飞应用id就好了】
5     NSString *appID = [NSString stringWithFormat:@"appid=%@",@"5750da61"];
6     [IFlySpeechUtility createUtility:appID];
7     
8     return YES;
9 }

到了第一部分的实现了,直接上代码【FirstViewController.m框架

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

实现第二部分【SecondViewController.m】ide

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