一、科大讯飞开放平台:
注册、登录之后创建新应用。
因为本项目只实现了语音听写,所以在SDK下载中心勾选语音听写单项SDK就可以了
开发平台选择iOS,应用选择你要实现语音听写的应用,然后点击"下载SDK"按钮
程序中会用到Appid,程序中导入的SDK一定是要与这个应用相关联的SDK,下载下来的SDK压缩包就是以Appid结尾命名的。
二、项目配置
官方文档:http://www.xfyun.cn/doccenter/iOS
1、添加静态库
按下图添加SDK所需要的iOS库,请注意libz.dylib,CoreTelephoney.framework不要遗漏。
注:如果使用的是离线识别,还需要增加libc++.dylib。
2、初始化
必须在初始化后才可以使用语音服务,初始化是异步过程,推荐在程序入口处调用。
Appid是应用的身份信息,具有唯一性,初始化时必须要传入Appid。可以从demo的Definition.h APPID_VALUE中查看此信息。Demo和SDK申请地址:http://xfyun.cn
在AppDelegate.m的didFinishLaunchingWithOptions:方法中初始化
先导入头文件:
#import "iflyMSC/IFlySpeechUtility.h"
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // Override point for customization after application launch.
3 // 登录科大讯飞语音平台
4 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", @"5750da0e"];
5 [IFlySpeechUtility createUtility:initString];
6 return YES;
7 }
3、实现语音听写(语音转换成文字):
功能实现步骤:
- 导入头文件
- 创建声音识别视图
- 指定声音识别后回调的代理对象
- 开启声音识别功能
- 在回调方法中处理声音识别后返回的对象
语音识别中的参数:
1 //第一步:引入库文件
2 //科大讯飞语音识别功能回调方法的接口文件
3 #import <iflyMSC/IFlyRecognizerViewDelegate.h>
4 //科大讯飞语音识别功能的声音识别视图
5 #import <iflyMSC/IFlyRecognizerView.h>
6 //科大讯飞语音识别功能中定义的常量
7 #import <iflyMSC/IFlySpeechConstant.h>
8
9 // 遵循代理协议
10 @interface FirstViewController ()<IFlyRecognizerViewDelegate>
11
12 @property (weak, nonatomic) IBOutlet UITextView *wordTextView;
13
14 /// 语音识别对象
15 @property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;
16
17 /// 可变字符串接收相关的结果
18 @property (nonatomic, copy) NSMutableString *resultStr;
19 @end
20
21 @implementation FirstViewController
22
23 - (void)viewDidLoad {
24 [super viewDidLoad];
25 /**
26 语音识别文字
27 */
28 // 初始化语音识别控件
29 self.iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
30 // 设置代理
31 self.iflyRecognizerView.delegate = self;
32
33 //设置语音识别结果应用为普通文本领域
34 [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
35 //设置前端点检测时间为6000ms
36 [self.iflyRecognizerView setParameter: @"6000" forKey:[IFlySpeechConstant VAD_BOS]];
37 //设置后端点检测时间为700ms
38 [self.iflyRecognizerView setParameter: @"700" forKey:[IFlySpeechConstant VAD_EOS]];
39 //设置采样率为8000
40 [self.iflyRecognizerView setParameter: @"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
41 //设置为返回结果中包含标点符号
42 [self.iflyRecognizerView setParameter: @"1" forKey:[IFlySpeechConstant ASR_PTT]];
43 //设置语音识别完成后数据的返回数据结构类型xml
44 [self.iflyRecognizerView setParameter: @"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
45 //设置在Documents文件夹下缓存的文件名为temp.asr
46 [self.iflyRecognizerView setParameter: @"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
47 //设置自定义的参数
48 [self.iflyRecognizerView setParameter: @"custom" forKey:[IFlySpeechConstant PARAMS]];
49
50
51 }
52
53 #pragma mark - 语音识别文字
54 - (IBAction)recognizeAction:(UIButton *)sender {
55
56 // 开始识别语音
57 [self.iflyRecognizerView start];
58
59 }
60
61
62 #pragma mark - 代理方法
63 /*!
64 * 回调返回识别结果
65 *
66 * @param resultArray 识别结果,NSArray的第一个元素为NSDictionary,NSDictionary的key为识别结果,sc为识别结果的置信度
67 * @param isLast -[out] 是否最后一个结果
68 */
69 // 成功
70 - (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast {
71 self.resultStr = [[NSMutableString alloc] init];
72 NSDictionary *dic = [resultArray objectAtIndex:0];
73
74 for (NSString *key in dic)
75 {
76 [self.resultStr appendFormat:@"%@",key];
77 }
78 NSLog(@"%@---------",_resultStr);
79
80 self.wordTextView.text = [NSString stringWithFormat:@"%@%@",self.wordTextView.text,self.resultStr];
81 }
82
83 /*!
84 * 识别结束回调
85 *
86 * @param error 识别结束错误码
87 */
88 // 失败
89 - (void)onError: (IFlySpeechError *) error {
90 NSLog(@"%@", error);
91 }
92
93 @end