最近需求说有语音搜索功能,查了下资料然后找到科大讯飞的一些资料,作为自己学习简单记录下来.
一.到讯飞开放平台(http://www.xfyun.cn/)下载SDK
按照demo中的步骤一步一步获取即可.打开demo运行就可实现语音听写功能.
二.根据项目需求需要实现的功能类似于京东的语音搜索,具体步骤如下:
1.创建工程,添加静态库
2.添加的方法
点击Link Binary With Libraries 选择"+",添加对应的库,对于iflyMSC.framework需要从下载好的sdks中的lib中导入(需要注意路径问题).
3.在.pch中导入头文件
//语音识别相关
#import <iflyMSC/IFlyRecognizerViewDelegate.h>
#import <iflyMSC/IFlyRecognizerView.h>
#import <iflyMSC/IFlySpeechConstant.h>
#import <iflyMSC/IFlySpeechUtility.h>
4.在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加如下代码:
此时的appid即为在讯飞配置项目时的appid.
5.在VC中的代码
#import "ViewController.h"
//声明代理
@interface ViewController ()<IFlyRecognizerViewDelegate>
//声明一个IFlyRecognizerView的属性.
@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;
@end
#pragma mark ======= 懒加载 =======
- (IFlyRecognizerView *)iflyRecognizerView{
if(!_iflyRecognizerView){
_iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
[self.view addSubview:_iflyRecognizerView];
}
return _iflyRecognizerView;
}
//语音转化为文字完成的时候,执行的方法
- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
NSLog(@"isLast:%d-----resultArray:%@",isLast,resultArray);
//1,先将 resultArray 的第一个元素取出(字典类型)
NSDictionary *dic = resultArray[0];
//2,由于有用的信息在 dic 的 key 值中,所以我们一会要取出 key 值.
NSString *jsonResult = [dic allKeys].lastObject;
//3,解析 jsonresult, 取出语音转换好的文字
NSString *resultString = [self transFromWithSouceString:jsonResult];
//将文字呈现在 testView 上
UITextView *textView = [self.view viewWithTag:1000];
if (textView.text) { //说明 textView 上已经有文字,那就直接拼接.
textView.text = [NSString stringWithFormat:@"%@%@",textView.text,resultString];
}else{//无文字,直接赋值
textView.text = resultString;
}
//取消\结束转换
[self.iflyRecognizerView cancel];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
//设置代理
self.iflyRecognizerView.delegate = self;
//设置的是当前主要功能 iat:将语音转化为文本
[self.iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
//设置的是语音存储位置 语音默认保存位置
[self.iflyRecognizerView setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
//在未启动语音功能时,隐藏界面
self.iflyRecognizerView.hidden = YES;
//启动按钮(添加按钮,在点击按钮时,调用方法)
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(100,100, 100, 50)];
[button setTitle:@"语音听写" forState: UIControlStateNormal];
[button addTarget:self action:@selector(startRecoder:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//初始化 testView, 用来显示识别好的文字
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
textView.backgroundColor = [UIColor redColor];
[self.view addSubview:textView];
textView.tag = 1000;//提供表示符.
}
#pragma mark-----回调按钮.
- (void)startRecoder:(UIButton*)sender{
UITextView *testView = [self.view viewWithTag:1000];
testView.text = @"";
//启动识别服务
[self.iflyRecognizerView start];
//让隐藏的界面展现出来
// self.iflyRecognizerView.hidden = NO;
}
//例如将以下文字的内容输出'' 今天的天气怎么样.'' {"sn":1,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"w":" 今天 ","sc":0}]},{"bg":0,"cw":[{"w":" 的","sc":0}]},{"bg":0,"cw":[{"w":" 天气 ","sc":0}]},{"bg":0,"cw":[{"w":" 怎么样 ","sc":0}]},{"bg":0,"cw":[{"w":" 。","sc":0}]}]}
- (NSString*)transFromWithSouceString:(NSString*)sourceString{
if (sourceString == nil) { //如果 sourceString 不存在,就不进行数据处理.
return @"";
}
//1.将 json 串转换为需要的字典类型.
/**
现将字符串转换为 NSData 类型
*/
NSData *dataSource= [sourceString dataUsingEncoding:NSUTF8StringEncoding];
//万一崩溃,先检查 source 是否有值
//将data 解析为字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataSource options:NSJSONReadingAllowFragments error:nil];
//先取出'ws'对应的值,
NSArray *wsArray = [dic objectForKey:@"ws"];
//声明一个可变字符串,用来拼接可变字符
NSMutableString *resultString = [[NSMutableString alloc] init];
//遍历wsArray,取出该数组当中的所有元素(字典)
for (NSDictionary *itemDic in wsArray) {
//itemDic 的数据结构->{"bg":0,"cw":[{"w":" 今天 ","sc":0}]}
/**
* 将 itemDic 当中的 cw的值取出,该值得类型为数组.
*/
NSArray *cwarray = [itemDic objectForKey:@"cw"];
//遍历 cwarray,取出该数组当中的所有元素(字典);
for (NSDictionary *cwdic in cwarray) {
//cwdic 的数据结构为{"w":" 今天 ","sc":0}
//1,先将 w 所对应的值取出
NSString *wString = cwdic[@"w"];
//将 w 的值拼接起来.拼接到 resultString 中
[resultString appendString:wString];
}
}
//将最终的字符返回
return resultString;
}
参考文章: