第一步:導(dǎo)入第三方
1.Baidu-Voice-SDK-iOS-1.6.2(百度語(yǔ)音)
2.JSONKit
3.libqrencode(二維碼生成)
第二步:添加依賴庫(kù)
1.CoreGraphics.framework
2.CFNetwork.framework
3.CoreLocation.framework
4.CoreText.framework
5.QuartzCore.framework
6.Security.framework
7.libz.tbd
8.AudioToolbox.framework
9.SystemConfiguration.framework
10.AVFoundation.framework
11.CoreTelephony.framework
12.GLKit.framework
13.libBDVoiceRecognitionClient.a
第三步:關(guān)閉ARC
Bulid Phases —> JSONKit.m
-fno-objc-arc
第四步:修改其他的連接器標(biāo)志
Bulid Settings —> Other Linker Flags
-ObjC
第五步:導(dǎo)入頭文件
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import “QRCodeGenerator.h"
第六步:代碼
@interface ViewController (){
? ? //語(yǔ)音界面
? ? BDRecognizerViewController *bdrv;
? ? NSMutableData *allData;
? ? //參數(shù)設(shè)置 key 秘鑰
? ? 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];
? ? //這里用一個(gè)button來(lái)實(shí)現(xiàn)
? ? // 點(diǎn)擊說(shuō)話
? ? UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? b.frame = CGRectMake(50, 200, 100, 30);
? ? [b setTitle:@"點(diǎn)擊說(shuō)話" forState:UIControlStateNormal];
? ? [b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:b];
? ? // 點(diǎn)擊清除
? ? UIButton *bu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? bu.frame = CGRectMake(250, 200, 100, 30);
? ? [bu setTitle:@"點(diǎn)擊清除" forState:UIControlStateNormal];
? ? [bu addTarget:self action:@selector(qingchu) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:bu];
? ? // 點(diǎn)擊生成二維碼
? ? UIButton *but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
? ? but.frame = CGRectMake(120, 500, 150, 30);
? ? [but setTitle:@"點(diǎn)擊生成二維碼" forState:UIControlStateNormal];
? ? [but addTarget:self action:@selector(erweima) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:but];
? ? //主題設(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";
}
//點(diǎn)擊說(shuō)話
-(void)click{
? ? allData = [[NSMutableData alloc]init];
? ? [bdrv startWithParams:bdvp];
}
// 點(diǎn)擊清除
-(void)qingchu{
? ? label.text = nil;
}
// 點(diǎn)擊生成二維碼
-(void)erweima{
? ? UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(120, 280, 200, 200)];
? ? imgV.backgroundColor = [UIColor purpleColor];
? ? [self.view addSubview:imgV];
? ? // 生成二維碼
? ? UIImage *img = [QRCodeGenerator qrImageForString:label.text imageSize:imgV.frame.size.width];
? ? // 把生成的二維碼展示在圖像框上
? ? imgV.image = img;
}
/**
* @brief 錄音數(shù)據(jù)返回
* @param recordData 錄音數(shù)據(jù)
* @param sampleRate 采樣率
*/
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
? ? [allData appendData:recordData];
}
//此方法是將語(yǔ)音傳遞到lable上
- (void)onPartialResults:(NSString *)results
{
? ? label.text = results;
}