//添加百度語音官網(wǎng)依賴庫網(wǎng)址:http://yuyin.baidu.com/docs/asr/180
//需要導(dǎo)入百度語音SDK官方庫文件或已下載好的庫到過程中,才能添加以下代碼到控制器中
#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "BDVRRawDataRecognizer.h"
#import "BDVRFileRecognizer.h"
@interface ViewController ()
{
? ? 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= [UIColorlightGrayColor];
? ? button.layer.cornerRadius = 10;
? ? [buttonsetTitle:@"點擊說話" forState:UIControlStateNormal];
? ? [buttonaddTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
? ? [self.viewaddSubview:button];
? ? BDTheme*me = [BDThemelightBlueTheme];
? ? //初始化
? ? bdvc = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(20, 100) withTheme:me];
? ? //設(shè)置代理
? ? 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= [[NSMutableDataalloc]init];
? ? [bdvc startWithParams:bdvp];
}
#pragma mark -- 代理方法
/**
?* @brief 語音識別結(jié)果返回,搜索和輸入模式結(jié)果返回的結(jié)構(gòu)不相同
?*
?* @param aBDRecognizerView 彈窗UI
?* @param aResults 返回結(jié)果聪建,搜索結(jié)果為數(shù)組,輸入結(jié)果也為數(shù)組树绩,但元素為字典
?*/
- (void)onEndWithViews:(BDRecognizerViewController*)aBDRecognizerViewController withResults:(NSArray*)aResults
{
? ? textV.text = [[[[aResults objectAtIndex:0]objectAtIndex:0]allKeys]objectAtIndex:0];
}
/**
?* @brief 錄音數(shù)據(jù)返回
?*
?* @param recordData 錄音數(shù)據(jù)
?* @param sampleRate 采樣率
?*/
- (void)onRecordDataArrived:(NSData*)recordData sampleRate:(int)sampleRate
{
? ? //拼接
? ? [MdataappendData:recordData];
}