#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "QRCodeGenerator.h"
@interface ViewController ()<BDRecognizerViewDelegate>
{
?BDRecognizerViewController *bdrv;
?NSMutableData *allData;
?BDRecognizerViewParamsObject *bdvp;
?UILabel *label;
}
@end
@implementation ViewController
- (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來實(shí)現(xiàn)
???
??? 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];
???
??? //主題設(shè)置
???
??? 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 不用設(shè)置
???
??? bdvp.apiKey = @"ANQLQINhgf2TL0gVP5xhNCxm";
???
??? bdvp.secretKey = @"c3d5f5f8ac5478e87802431389b2cba7";
???
}
//button方法
-(void)click{
???
??? allData = [[NSMutableData alloc]init];
???
??? [bdrv startWithParams:bdvp];
???
}
/**
?
?* @brief 錄音數(shù)據(jù)返回
?
?* @param recordData 錄音數(shù)據(jù)
?
?* @param sampleRate 采樣率
?
?*/
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
???
??? [allData appendData:recordData];
???
}
//此方法是將語音傳遞到lable上
- (void)onPartialResults:(NSString *)results
{
???
??? label.text = results;
???
}
-(void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerViewController withResults:(NSArray *)aResults{
??? label.text=[[[[aResults lastObject]lastObject]allKeys]lastObject];
???
??? UIImageView * img =[[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 150, 150)];
??? img.image=[QRCodeGenerator qrImageForString:[[[[aResults lastObject]lastObject]allKeys]lastObject] imageSize:self.view.frame.size.width];
??? [self.view addSubview:img];
??? [self.view reloadInputViews];
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
??? // Dispose of any resources that can be recreated.
}
@end
值得注意的是绝编。百度語音需要導(dǎo)入幾個類庫垄琐。不然的話容易報(bào)錯