第一步:导入第三方
1.Baidu-Voice-SDK-iOS-1.6.2(百度语音)
2.JSONKit
3.libqrencode(二维码生成)
第二步:添加依赖库
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
第三步:关闭ARC
Bulid Phases —> JSONKit.m
-fno-objc-arc
第四步:修改其他的连接器标志
Bulid Settings —> Other Linker Flags
-ObjC
第五步:导入头文件
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import “QRCodeGenerator.h"
第六步:代码
@interface ViewController (){
//语音界面
BDRecognizerViewController *bdrv;
NSMutableData *allData;
//参数设置 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];
//这里用一个button来实现
// 点击说话
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(50, 200, 100, 30);
[b setTitle:@"点击说话" forState:UIControlStateNormal];
[b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
// 点击清除
UIButton *bu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bu.frame = CGRectMake(250, 200, 100, 30);
[bu setTitle:@"点击清除" forState:UIControlStateNormal];
[bu addTarget:self action:@selector(qingchu) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bu];
// 点击生成二维码
UIButton *but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame = CGRectMake(120, 500, 150, 30);
[but setTitle:@"点击生成二维码" forState:UIControlStateNormal];
[but addTarget:self action:@selector(erweima) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
//主题设置
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";
}
//点击说话
-(void)click{
allData = [[NSMutableData alloc]init];
[bdrv startWithParams:bdvp];
}
// 点击清除
-(void)qingchu{
label.text = nil;
}
// 点击生成二维码
-(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 录音数据返回
* @param recordData 录音数据
* @param sampleRate 采样率
*/
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
[allData appendData:recordData];
}
//此方法是将语音传递到lable上
- (void)onPartialResults:(NSString *)results
{
label.text = results;
}
网友评论