大家好,我是RickyWei。
語音識(shí)別有很多種谚鄙,今天給大家?guī)淼氖歉骺剑俣日Z音識(shí)別 。
一. 引入編譯需要的Framework
1.BDVRClient使用了錄音和播放功能闷营,因此需要在Xcode工程中引入AudioToolbox.framework和AVFoundation.framework烤黍;BDVRClient還使用到了網(wǎng)絡(luò)狀態(tài)檢測功能,因此還需要引入SystemConfiguration.framework粮坞;為了生成設(shè)備UDID蚊荣,需要引入Security.framework;為了支持gzip壓縮莫杈,需要引入libz.1.dylib; 網(wǎng)絡(luò)模塊需要引入CFNetwork.framework互例;某些場景需要獲取設(shè)備地理位置以提高識(shí)別準(zhǔn)確度,需引入CoreLocation.framework筝闹。
為了支持識(shí)別控件媳叨,需要引入OpenGLES.framework,QuartzCore.framework关顷,GLKit.framework糊秆,CoreGraphics.framework和CoreText.framework。
添加方式:右鍵點(diǎn)擊Xcode中的工程文件议双,在出現(xiàn)的界面中痘番,選中TARGETS中應(yīng)用,在出現(xiàn)的界面中選中Build Phase->Link Binary With Libraries平痰,點(diǎn)擊界面中的“+”圖標(biāo)汞舱,在彈出的界面中選擇這些Framework即可,添加完成效果圖如圖所示宗雇。
2.將在百度語音開房平臺(tái)上下載的SDK拖入到項(xiàng)目工程的文件目錄下
3.刪除BDVRClientSample文件夾
4.如圖 向項(xiàng)目中添加文件夾
4.1 將文件夾BDVoiceRecognitionClientResources從項(xiàng)目中移除 再添加?
添加模式如下:
4.2 修改后文件夾目錄如下:
二.引入BDVRClient的頭文件
1.首先將BDVRClient提供的頭文件拷貝到工程目錄下昂芜,在XCode中添加此文件,引入BDVRClient提供的頭文件赔蒲。
2.1 如果使用識(shí)別UI泌神,請?zhí)砑尤缦骂^文件(本文只使用識(shí)別UI和接口):
1.#import "BDRecognizerViewController.h"
?2.#import "BDRecognizerViewDelegate.h"
2.2 如果只使用識(shí)別接口良漱,添加如下頭文件:
1.#import "BDVoiceRecognitionClient.h"
2.3 如果要對音頻數(shù)據(jù)或音頻文件直接進(jìn)行識(shí)別,請分別添加如下頭文件:
1.#import "BDVRRawDataRecognizer.h"
2.#import "BDVRFileRecognizer.h"
添加完后如圖:
2.4 控件代碼如下:
apiKey與secretKey 為百度語音識(shí)別應(yīng)用給的key?
#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
@interface ViewController (){
//語音界面
BDRecognizerViewController *bdrv;
NSMutableData *allData;
//參數(shù)設(shè)置 Key 密鑰
BDRecognizerViewParamsObject *bdvp;
}
//這是直接拉的一個(gè)text View
@property (weak, nonatomic) IBOutlet UITextView *textLable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加一個(gè)按鈕
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(100, 400, 100, 30);
[b setTitle:@"開始識(shí)別" forState:UIControlStateNormal];
[b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
//主題設(shè)置
BDTheme *me = [BDTheme lightOrangeTheme];
bdrv = [[BDRecognizerViewController alloc]initWithOrigin:CGPointMake(20, 10) withTheme:me];
//設(shè)置為全屏幕
bdrv.enableFullScreenMode = YES;
bdrv.delegate = self;
bdvp = [[BDRecognizerViewParamsObject alloc]init];
//bdvp.productID 不用設(shè)置
bdvp.apiKey = @"0VoiTxX42brGtyp3P330cONO";
bdvp.secretKey = @"5b929494d330b6d57385c7539628cccd";
}
-(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];
}
/**
* @brief 返回中間識(shí)別結(jié)果
*
* @param results
*? ? ? ? ? ? 中間識(shí)別結(jié)果
*/
- (void)onPartialResults:(NSString *)results{
self.textLable.text = results;
}
//這個(gè)可以選擇性刪除
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
三.添加第三方開源庫
?3.1 ? ? BDVRClient中使用了第三方開源庫欢际,包括TTTAttributedLabel和蘋果非官方的錄音API母市, ?如果產(chǎn)品項(xiàng)目中已經(jīng)包含其中的部分庫,請勿重復(fù)添加损趋,否則窒篱,請?zhí)砑舆@三種第三方開源庫到項(xiàng)目中,第三方庫文件可以在SDK開發(fā)包下的Third-party目錄下找到舶沿。由于SDK中使用了類別擴(kuò)展,請?jiān)贐uild Setting中的Other Linker Flags中添加-ObjC配并。
注意:其中第三方庫TTTAttributedLabel需要設(shè)置為ARC方式編譯括荡。
3.2 同時(shí)不要忘記要把JSONKit關(guān)閉 ARC