最近公司做项目,需要用到语音识别,所以自己根据百度云文档以及从网上查找的一些资源作出以下总结:
一,准备工作
1,新建一个项目
2,在百度云开放平台创建应用,控制台>产品服务>人工智能>百度语音
3,创建完应用后获得APPID,APPKEY,APP秘密。
二,配置环境
1,下载百度云语音识别SDK文件及三方库文件,并将文件导入工程,注意:BDVoiceRecognitionClientResources文件选择创建文件夹参考这一项如下图所示
2,导入依赖库文件,如图所示
3,打开目标下建立设置,双击其他链接器标志,添加-ObjC,启用位码设置为否。
如图4所示,打开构建阶段下编译源,双击BDVRSConfig.m文件,添加-fno-objc弧设置为弧可访问。
三,最爱的上代码
#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDVRSConfig.h"
const NSString* API_KEY = @"v9y5Rr9qVRzx0ghomfqucP76";
const NSString* SECRET_KEY = @"92EFloWuYlGqwc2cXVoNTdc9VZBLcqwW";
const NSString* APP_ID = @"11678423";
@interface ViewController ()<BDRecognizerViewDelegate>
//创建识别控件
@property (nonatomic, strong) BDRecognizerViewController *recognizerViewController;
@property(nonatomic,strong)UIButton *voiceRecogButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//在线语音识别
[self createUI];
}
- (void)createUI{
self.voiceRecogButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 80, 375, 40)];
self.voiceRecogButton.backgroundColor = [UIColor redColor];
[self.voiceRecogButton setTitle:@"开始语音识别" forState:UIControlStateNormal];
[self.voiceRecogButton addTarget:self action:@selector(startVoice:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.voiceRecogButton];
}
- (void)startVoice:(UIButton *)btn{
//创建临时识别控件
BDRecognizerViewController *tmpRecognizerViewController = [[BDRecognizerViewController alloc]initWithOrigin:CGPointMake(40, 120) withTheme:[BDVRSConfig sharedInstance].theme];
//全屏UI
if ([[BDVRSConfig sharedInstance].theme.name isEqualToString:@"全屏亮蓝"]) {
tmpRecognizerViewController.enableFullScreenMode = YES;//全屏模式
}
//设置代理
tmpRecognizerViewController.delegate = self;
//将临时识别控件赋给全局识别控件
self.recognizerViewController = tmpRecognizerViewController;
//设置识别参数
BDRecognizerViewParamsObject *paramObject = [[BDRecognizerViewParamsObject alloc]init];
//开发者信息(在百度云开放平台创建应用获得)
paramObject.apiKey = API_KEY;
paramObject.secretKey = SECRET_KEY;
//设置是否需要语义理解,只在搜索模式有效
paramObject.isNeedNLU = [BDVRSConfig sharedInstance].isNeedNLU;
//设置识别语言
paramObject.language = [BDVRSConfig sharedInstance].recognitionLanguage;
//设置识别模式,分为搜索和输入
paramObject.recogPropList = @[[BDVRSConfig sharedInstance].recognitionProperty];
//设置城市ID,当识别属性包含EVoiceRecognitionPropertyMap时有效
paramObject.cityID = 1;
//开启联系人识别
// paramObject.enableContacts = YES;
//设置显示效果,是否开启连续上屏
if ([BDVRSConfig sharedInstance].resultContinuousShow) {
paramObject.resultShowMode = BDRecognizerResultShowModeContinuousShow;
}else{
paramObject.resultShowMode = BDRecognizerResultShowModeWholeShow;
}
//设置提示音开关是否打开,默认打开
if ([BDVRSConfig sharedInstance].uiHintMusicSwitch) {
paramObject.recordPlayTones = EBDRecognizerPlayTonesRecordPlay;
}else{
paramObject.recordPlayTones = EBDRecognizerPlayTonesRecordForbidden;
}
paramObject.isShowTipAfter3sSilence = NO;
paramObject.isShowHelpButtonWhenSilence = NO;
// paramsObject.tipsTitle = @"可以使用如下指令记账";
// paramsObject.tipsList = [NSArray arrayWithObjects:@"我要记账", @"买苹果花了十块钱", @"买牛奶五块钱", @"第四行滚动后可见", @"第五行是最后一行", nil];
paramObject.appCode = APP_ID;
//识别模型模拟路径
paramObject.datFilePath = [[NSBundle mainBundle] pathForResource:@"s_1" ofType:@""];
if ([[BDVRSConfig sharedInstance].recognitionProperty intValue] == EVoiceRecognitionPropertyMap) {
paramObject.LMDatFilePath = [[NSBundle mainBundle] pathForResource:@"s_2_Navi" ofType:@""];
} else if ([[BDVRSConfig sharedInstance].recognitionProperty intValue] == EVoiceRecognitionPropertyInput) {
paramObject.LMDatFilePath = [[NSBundle mainBundle] pathForResource:@"s_2_InputMethod" ofType:@""];
}
paramObject.recogGrammSlot = @{@"$name_CORE" : @"张三\n李四\n",
@"$song_CORE" : @"小苹果\n朋友\n",
@"$app_CORE" : @"QQ\n百度\n微信\n百度地图\n",
@"$artist_CORE" : @"刘德华\n周华健\n"};
//开始识别
[self.recognizerViewController startWithParams:paramObject];
}
#pragma mark - delegate mathod
//返回语音识别结果
- (void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerViewController withResults:(NSArray *)aResults{
if ([[BDVoiceRecognitionClient sharedInstance] getRecognitionProperty] != EVoiceRecognitionPropertyInput) {
NSMutableArray *audioResultData = (NSMutableArray *)aResults;
NSMutableString *tmpString = [[NSMutableString alloc]initWithString:@""];
for (int i = 0; i < [audioResultData count]; i ++) {
[tmpString appendFormat:@"%@\r\n",[audioResultData objectAtIndex:i]];
}
NSLog(@"识别结果: %@",tmpString);
[self showWithResultString:tmpString];
}else{
NSString *tmpString = [[BDVRSConfig sharedInstance]composeInputModeResult:aResults];
NSLog(@"识别结果: %@",tmpString);
[self showWithResultString:tmpString];
}
}
- (void)showWithResultString:(NSString *)resultStr{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:resultStr preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
四,演示百度云下载地址:百度云语音识别 密码:x94g
下篇:语音合成
如果你觉得本文对你有所帮助,帮我点个小心心,鼓励我继续创作。