1. 打开网络连接权限 (不会看我以前文章) 在工程左侧创建一个文件夹 option+command+n 命名为SDK
2. 选中SDK文件夹 show in finder 打开Finder目录
3. 把文件(名字为BDvoiceSDK , 里面有两个文件) 里面的两个文件复制粘贴到SDK目录里 (BDvoiceSDK文件自取 , 在我的资源里)
4. 选择SDK文件 ADD Files to "工程名" , 然后选择BDvoiceSDK文件内的两个文件 !(124) 注意 千万不要选择SDK文件内的那两个 否则会报错!
5. 添加完两个文件后 编译一下 然后点击工程左侧第一个蓝文件 (名称是你自己的工程名) 点击 Build Phases 选择Compile Sources 找到JSONKit.m 添加 -fno-objc-arc
6. 接下来添加类库 类库如下
libBDVoiceRecognitionClient.a (这个会自己就有)
libc++.tbd
libz.1.2.5.tbd
AudioToolbox. framework
AVFoundation. framework
CFNetwork.framework
CoreLocation. framework
CoreTelephony. framework
SystemConfiguration. framework
libBDVoiceRecognitionClient.a
GLKit.framework
7. 添加完类库后 编译程序 然后 打开BDvoiceSDK(文件夹) -> Baidu-Voice-SDK-iOS-1.6.2 -> libBDVoiceRecognitionClient 按照顺序依次打开 会看见一个命名为 : “libBDVoiceRecognitionClient” 的文件 直接拖入到工程左侧(124) 编译 没有报错
8. 接下来开始代码部分 ViewController.m代码如下
#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "BDVRRawDataRecognizer.h"
#import "BDVRFileRecognizer.h"
@interface ViewController ()<BDRecognizerViewDelegate>
{
BDRecognizerViewController *bdvc;
NSMutableData *Mdata;
BDRecognizerViewParamsObject *bdvp;
UITextView *textV;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 30);
button.backgroundColor = [UIColor lightGrayColor];
button.layer.cornerRadius = 10;
[button setTitle:@"点击说话" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
BDTheme *me = [BDTheme lightBlueTheme];
bdvc = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(20, 100) withTheme:me];
bdvc.delegate = self;
bdvc.enableFullScreenMode = YES;
bdvp = [[BDRecognizerViewParamsObject alloc] init];
bdvp.apiKey = @"S0UrbTi31Yi6hTGjssCk0hzR";
bdvp.secretKey = @"adb44d9f61aa57d86bcf970572c2f568";
textV = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
textV.layer.borderWidth = 1;
[self.view addSubview:textV];
}
- (void)clickButton
{
Mdata = [[NSMutableData alloc] init];
[bdvc startWithParams:bdvp];
}
#pragma mark -- 代理方法
/**
* @brief 语音识别结果返回,搜索和输入模式结果返回的结构不相同
*
* @param aBDRecognizerView 弹窗UI
* @param aResults 返回结果,搜索结果为数组,输入结果也为数组,但元素为字典
*/
- (void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerViewController withResults:(NSArray *)aResults
{
textV.text = [[[[aResults objectAtIndex:0]objectAtIndex:0]allKeys]objectAtIndex:0];
}
/**
* @brief 录音数据返回
*
* @param recordData 录音数据
* @param sampleRate 采样率
*/
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate
{
[Mdata appendData:recordData];
}
@end