可參照iOS百度語音識別開發(fā)文檔完成相關配置
一、接入指南
先引入編譯需要的Framework:
AudioToolbox.framework
AVFoundation.framework
SystemConfiguration.framework
CoreTelephony.framework
Security.framework
libz.1.tbd
CFNetwork.framework
CoreLocation.framework
OpenGLES.framework
QuartzCore.framework
GLKit.framework
CoreGraphics.framework
CoreText.framework?
注意事項:
1.注意 JSONKit.m 使用非arc方式編譯,需在Build Phases-> Compile Sources->JSONKit.m 的 Compiler Flags 中添加 -fno-objc-arc, 若在xcode7上編譯過程中,運行到 JSONKit文件中這一句
void? *objectISA? = (JK_EXPECT_F(workAroundMacOSXABIBreakingBug)) ? NULL : *((void **)objectPtr); ?的時候程序就崩潰,那么 JSONKit 的版本有點低,下載最新的JSONKit文件將其替換即可;
2.設置App Transport Security,在項目的info.plist中,添加NSAppTransportSecurity,然后在NSAppTransportSecurity 下選擇 Allow Arbitrary Loads,類型Boolean,值設為YES。
3.在BuildSettings中,設置Enable Bitcode為NO;
二、使用
在此僅使用的是BDVoiceRecognitionClient來進行語音識別。
1.引入頭文件
#import "BDVoiceRecognitionClient.h"
2.配置 BDVoiceRecognitionClient
// 使用自己申請的應用的ApiKey和SecretKey替換之
[[BDVoiceRecognitionClient sharedInstance]setApiKey:@"ApiKey"withSecretKey:@"SecretKey"];
[[BDVoiceRecognitionClient sharedInstance] setLanguage:EVoiceRecognitionLanguageChinese];
[[BDVoiceRecognitionClient sharedInstance] setResourceType:RESOURCE_TYPE_NLU];
[[BDVoiceRecognitionClient sharedInstance] setPropertyList:@[@(EVoiceRecognitionPropertyWeb)]];
if ([[BDVoiceRecognitionClient sharedInstance] isCanRecorder]) {
[[BDVoiceRecognitionClient sharedInstance] startVoiceRecognition:self];
}
3.遵守MVoiceRecognitionClientDelegate協(xié)議,實現(xiàn)以下代理方法
根據(jù)不同的字段,可自行對結果進行處理
- (void)VoiceRecognitionClientWorkStatus:(int) aStatus obj:(id)aObj{
? ? ? ? switch (aStatus) {
? ? ? ? ? ? ? ? ? ? case EVoiceRecognitionClientWorkStatusStartWorkIng:
? ? ? ? ? ? ? ? ? ? NSLog(@"開始錄音");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ?case EVoiceRecognitionClientWorkStatusStart:
? ? ? ? ? ? ? ? ? ?NSLog(@"檢測到用戶開始說話");
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case EVoiceRecognitionClientWorkStatusEnd:
? ? ? ? ? ? ? ? ? ?NSLog(@"結束錄音");
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case EVoiceRecognitionClientWorkStatusFinish:
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ?// 此處返回識別結果
? ? ? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"語音識別功能完成,服務器返回正確結果");
? ? ? ? ? ? ? ? ? ? ? ? ? ?NSString * string = [aObj JSONString];
? ? ? ? ? ? ? ? ? ? ? ? ? ?NSLog(@"識別結果: %@",string);
? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? break;
? ? ? ? ?}
}
- (void)VoiceRecognitionClientErrorStatus:(int) aStatus subStatus:(int)aSubStatus{
? ? ? ? ? ? ? ? switch (aStatus) {
? ? ? ? ? ? ? ? case EVoiceRecognitionClientErrorStatusClassVDP:
? ? ? ? ? ? ? ? NSLog(@"語音數(shù)據(jù)處理過程出錯");
? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ?case EVoiceRecognitionClientErrorStatusUnKnow:
? ? ? ? ? ? ? ?NSLog(@"未知錯誤(異常)");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case EVoiceRecognitionClientErrorStatusNoSpeech:
? ? ? ? ? ? ? ?NSLog(@"用戶未說話");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case EVoiceRecognitionClientErrorStatusShort:
? ? ? ? ? ? ? NSLog(@"用戶說話聲音太短");
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? case EVoiceRecognitionClientErrorStatusException:
? ? ? ? ? ? ? NSLog(@"語音前端庫檢測異常");
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? case EVoiceRecognitionClientErrorStatusClassRecord:
? ? ? ? ? ? ?NSLog(@"錄音出錯");
? ? ? ? ? ? ?break;
? ? ? ? ? ? ?case EVoiceRecognitionClientErrorStatusClassLocalNet:
? ? ? ? ? ? ?NSLog(@"本地網(wǎng)絡聯(lián)接出錯");
? ? ? ? ? ? ?break;
? ? ? ? ? ? ?case EVoiceRecognitionClientErrorStatusClassServerNet:
? ? ? ? ? ? ?NSLog(@"服務器返回網(wǎng)絡錯誤");
? ? ? ? ? ? ?break;
? ? ? ? ? ? ?default:
? ? ? ? ? ? ?break;
? ? ? ? ? ? }
}
- (void)VoiceRecognitionClientNetWorkStatus:(int) aStatus{
? ? ? ? ? ? ?switch (aStatus) {
? ? ? ? ? ? ?case EVoiceRecognitionClientNetWorkStatusStart:
? ? ? ? ? ? ?NSLog(@"網(wǎng)絡工作開始");
? ? ? ? ? ? ?break;
? ? ? ? ? ? ?case EVoiceRecognitionClientNetWorkStatusEnd:
? ? ? ? ? ? ? NSLog(@"網(wǎng)絡工作完成");
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? default:
? ? ? ? ? ? ? break;
? ? ? ? ? ? ?}
}