效果是把识别的语音展示在textFiled上
以下是官方集成文档里面有的代码,目的是导入支持的百度语音的内置功能
#import "BDSEventManager.h"
#import "BDSASRDefines.h"
#import "BDSASRParameters.h"
#import "BDTheme.h"
#import "BDRecognizerViewParamsObject.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
以下是官方集成文档里面有的代码,是导入自己创建项目的各种数据
const NSString* API_KEY = @"mFCLdfgYO4C8bebaTxtgaYvU";
const NSString* SECRET_KEY = @"yuHUuEQE3pBhCI5t78BCas6xqK82gUQk";
const NSString* APP_ID =@"15204631";
定义属性
@property (strong, nonatomic) BDSEventManager *asrEventManager;//注册应用
@property(nonatomic,strong)BDRecognizerViewController*recognizerViewController;//语音识别控制器
@property(nonatomic,strong)UITextField * inputTF;//定义一个文本框
@property(nonatomic,strong)UIButton * btn;//定义一个开始语音按钮
以下是viewDiedload里面的代码
- (void)viewDidLoad {
[super viewDidLoad];
//初始化文本框的位置信息
self.inputTF=[[UITextField alloc]initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 40)];
//设置文本框的边框的样式
self.inputTF.borderStyle=UITextBorderStyleRoundedRect;
//把文本输入框添加到主视图上
[self.viewaddSubview:self.inputTF];
//初始化语音输入按钮的位置信息
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, self.view.frame.size.width-200, 40)];
//设置按钮的背景颜色
self.btn.backgroundColor=[UIColor redColor];
//设置按钮的标题
[self.btn setTitle:@"开始语音识别" forState:UIControlStateNormal];
//为按钮添加事件
[self.btn addTarget:self action:@selector(voiceRecgnize:) forControlEvents:UIControlEventTouchUpInside];
//按钮添加到主视图
[self.viewaddSubview:self.btn];
self.asrEventManager = [BDSEventManager createEventManagerWithName:BDS_ASR_NAME];
//设置DEBUG_LOG的级别
[self.asrEventManager setParameter:@(EVRDebugLogLevelTrace) forKey:BDS_ASR_DEBUG_LOG_LEVEL];
//配置API_KEY 和 SECRET_KEY 和 APP_ID
[self.asrEventManager setParameter:@[API_KEY, SECRET_KEY] forKey:BDS_ASR_API_SECRET_KEYS];
[self.asrEventManager setParameter:APP_ID forKey:BDS_ASR_OFFLINE_APP_CODE];
//配置端点检测(二选一)
NSString*modelVAD_filepath = [[NSBundle mainBundle]pathForResource:@"bds_easr_basic_model"ofType:@"dat"];
[self.asrEventManager setParameter:modelVAD_filepath forKey:BDS_ASR_MODEL_VAD_DAT_FILE];
[self.asrEventManager setParameter:@(YES) forKey:BDS_ASR_ENABLE_MODEL_VAD];
// [self configDNNMFE];
// [self.asrEventManager setParameter:@"15361" forKey:BDS_ASR_PRODUCT_ID];
// ---- 语义与标点 -----
// ---- 开启语义理解 -----
[self.asrEventManager setParameter:@(YES) forKey:BDS_ASR_ENABLE_NLU];
[self.asrEventManager setParameter:@"1536" forKey:BDS_ASR_PRODUCT_ID];
}
以下是触发按钮的方法
-(void)voiceRecgnize:(UIButton* )sender
{
[self.asrEventManager setParameter:@"" forKey:BDS_ASR_AUDIO_FILE_PATH];
BDRecognizerViewParamsObject *paramsObject = [[BDRecognizerViewParamsObject alloc] init];
paramsObject.isShowTipAfterSilence = YES;
paramsObject.isShowHelpButtonWhenSilence = NO;
paramsObject.tipsTitle=@"您可以这样问";
paramsObject.tipsList= [NSArray arrayWithObjects:@"我要吃饭",@"我要买电影票",@"我要订酒店",nil];
paramsObject.waitTime2ShowTip=0.5;
paramsObject.isHidePleaseSpeakSection = YES;
paramsObject.disableCarousel=YES;
self.recognizerViewController = [[BDRecognizerViewController alloc] initRecognizerViewControllerWithOrigin:CGPointMake(9, 80)
theme:nil
enableFullScreen:YES
paramsObject:paramsObject
delegate:self];
[self.recognizerViewController startVoiceRecognition];
}
以下是语音识别结束后回调的方法,可以在此方法中获取语音识别结果,协议的回调方法,以下有遵守协议的代理
- (void)onEndWithViews:(BDRecognizerViewController*)aBDRecognizerViewController withResult:(id)aResult
{
NSLog(@"识别结果%@",aResult);
NSString* res=[aResult[@"results_recognition"]firstObject];
dispatch_async(dispatch_get_main_queue(), ^{
self.inputTF.text=res;
});
}
- (void)onError:(int)errClass errCode:(int)errCode errDescription:(NSString*)errDescription
{
NSLog(@"识别错误,error====%@",errDescription);
}
遵守协议
<BDRecognizerViewDelegate>
导入依赖库支持
导入sdk时候回报错,看看有没有.a文件,如果有就删掉
需要在info.plist文件中写入使用话筒的权限
Privacy - Microphone Usage Description 请你打开您的话筒权限,以便更精确的使用语音功能