请参考《百度语音开放平台使用指南》创建应用,开通服务并完成个性化设置。
引入编译需要的Framework
BDVRClient使用了录音和播放功能,因此需要在Xcode工程中引入AudioToolbox.framework和AVFoundation.framework;BDVRClient还使用到了网络状态检测功能,因此还需要引入SystemConfiguration.framework;为了生成设备UDID,需要引入Security.framework;为了支持gzip压缩,需要引入libz.1.dylib; 网络模块需要引入CFNetwork.framework;某些场景需要获取设备地理位置以提高识别准确度,需引入CoreLocation.framework。
为了支持识别控件,需要引入OpenGLES.framework,QuartzCore.framework,GLKit.framework,CoreGraphics.framework和CoreText.framework。
添加方式:右键点击Xcode中的工程文件,在出现的界面中,选中TARGETS中应用,在出现的界面中选中Build Phase->Link Binary With Libraries,点击界面中的“+”图标,在弹出的界面添加如下Framework即可,添加完成效果图如图所示(libBDVoiceRecognitionClient.a将在随后添加)。
把下载的SDK以及JSON文件导入Dome
注意 : 将JSON文件关闭ARC
二.引入BDVRClient的头文件
1.首先将BDVRClient提供的头文件拷贝到工程目录下,在XCode中添加此文件,引入BDVRClient提供的头文件。
2.1 如果使用识别UI,请添加如下头文件(本文只使用识别UI和接口):
1.#import “BDRecognizerViewController.h”
2.#import “BDRecognizerViewDelegate.h”
2.2 如果只使用识别接口,添加如下头文件:
1.#import “BDVoiceRecognitionClient.h”
2.3 如果要对音频数据或音频文件直接进行识别,请分别添加如下头文件:
1.#import “BDVRRawDataRecognizer.h”
2.#import “BDVRFileRecognizer.h
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
@interface ViewController ()<BDRecognizerViewDelegate>
{
BDRecognizerViewController *bdrv;
NSMutableData *allData;
BDRecognizerViewParamsObject *bdvp;
UILabel *label;
}
@end
三.添加第三方开源库
3.1 BDVRClient中使用了第三方开源库,包括TTTAttributedLabel和苹果非官方的录音API, 如果产品项目中已经包含其中的部分库,请勿重复添加,否则,请添加这三种第三方开源库到项目中,第三方库文件可以在SDK开发包下的Third-party目录下找到。由于SDK中使用了类别扩展,请在Build Setting中的Other Linker Flags中添加-ObjC。
注意:其中第三方库TTTAttributedLabel需要设置为ARC方式编译。
接下来就是代码了 (写在ViewController里面)
@interface ViewController ()<BDRecognizerViewDelegate>
{
//语音界面
BDRecognizerViewController *bdrv;
NSMutableData *allData;
//参数设置 key 秘钥
BDRecognizerViewParamsObject *bdvp;
UILabel *label;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Lable初始化
label = [[UILabel alloc]initWithFrame:CGRectMake(50,100,300, 50)];
label.backgroundColor = [UIColor blueColor];
[self.view addSubview:label];
//这里用一个button来实现
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(100, 400, 100, 30);
[b setTitle:@"click" forState:UIControlStateNormal];
[b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
//主题设置
BDTheme *me = [BDTheme lightGreenTheme];
bdrv = [[BDRecognizerViewController alloc]initWithOrigin:CGPointMake(20, 180) withTheme:me];
//全屏幕
bdrv.enableFullScreenMode = YES;
bdrv.delegate = self;
bdvp = [[BDRecognizerViewParamsObject alloc]init];
//bdvp.productID 不用设置
bdvp.apiKey = @"ANQLQINhgf2TL0gVP5xhNCxm";
bdvp.secretKey = @"c3d5f5f8ac5478e87802431389b2cba7";
}
//button方法
-(void)click{
allData = [[NSMutableData alloc]init];
[bdrv startWithParams:bdvp];
}
/**
* @brief 录音数据返回
* @param recordData 录音数据
* @param sampleRate 采样率
*/
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
[allData appendData:recordData];
}
//此方法是将语音传递到lable上
- (void)onPartialResults:(NSString *)results
{
label.text = results;
}