1.导入百度语音的SDK
2.导入头文件:
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
3.创建对象及页面所需控件(并把协议协商)
{
//语音界面
BDRecognizerViewController * bdrv;
NSMutableData * allData;
//参数设置 key 秘钥
BDRecognizerViewParamsObject * bdvp;
UILabel * label;
}
4.ViewDidLoad里的:
//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 = @"f8LM61EyDFMQYYXVshiosbbe";
bdvp.secretKey = @"H1dXajmVmQh2p1SbpW8ZnEvX8SXSgZUL";
5.button方法
- (void) click {
allData = [[NSMutableData alloc]init];
[bdrv startWithParams : bdvp];
}
6.
- (void) onRecordDataArrived:(NSData*)recordData sampleRate:(int)sampleRate{
[allData appendData:recordData];
}
//此方法是将语音传递到lable上
- (void) onPartialResults:(NSString*)results
{
label.text= results;
}