一、工程配置
1.首先将SDK文件添加到项目中
WXVoiceSDK.h //语音识别
WXSpeechSynthesizer.h //语音合成
libWXVoiceSDK.a //库文件(必选)
WXSpeechRecognizerWithUI.h //语音识别+UI
WXResourceBundle.bundle //UI资源
2. 引入系统库
AudioToolbox.framework
SystemConfiguration.framework
CoreTelephony.framework
AVFoundation.framework
libz.dylib
3.设置Build Settings
C++ Standard Library: libstdc++ (GNU C++ standard library)
这里要选择libstdc++ (GNU C++ standard library),选择其他的可能会编译报错,
同时系统自动添加了 Social.framework 和 libc++.tbd,若没有手动添加。
将一个.m文件的扩展名改为.mm。
二、语音识别SDK调用流程
1.在这里,我只使用了语音识别的功能,引入头文件
#import "WXVoiceSDK.h"
2.初始化设置:
- (void)WXVoiceInit{
WXVoiceSDK * speechRecognizer = [WXVoiceSDK sharedWXVoice];
speechRecognizer.delegate = self;
speechRecognizer.silTime = 0.5f;
// 测试码: 248b63f1ceca9158ca88516bcb338e82a482ecd802cbca12
[speechRecognizer setUserKey:@"248b63f1ceca9158ca88516bcb338e82a482ecd802cbca12"]; //48位认证码
}
3.遵守WXVoiceDelegate协议,实现以下方法;
//识别成功,返回结果,元素类型为WXVoiceResult,现阶段数组内只有一个元素
- (void)voiceInputResultArray:(NSArray *)array{
if (array && array.count > 0) {
WXVoiceResult * result = [array objectAtIndex:0];
NSLog(@"识别结果:%@",result.text);
self.messageLabel.text = [NSString stringWithFormat:@"识别结果:%@",result.text];
[[WXVoiceSDK sharedWXVoice]cancel];
}
}
//出现错误,错误码请参见官方网站 错误码说明
- (void)voiceInputMakeError:(NSInteger)errorCode{
switch (errorCode) {
case WXErrorOfSuccess:
NSLog(@"识别成功");
break;
case WXErrorOfNoNetWork:
NSLog(@"没有网络");
break;
case WXErrorOfTimeOut:
NSLog(@"网络超时");
break;
case WXErrorOfQuotaExhaust:
NSLog(@"AppID配额用完");
break;
case WXErrorOfAppIDError:
NSLog(@"AppID错误");
break;
case WXVoiceRecoErrorOfNoSound:
NSLog(@"没有获取到录音");
break;
case WXVoiceRecoErrorOfVoiceTooLarge:
NSLog(@"录音过长");
break;
case WXVoiceRecoErrorOfRecord:
NSLog(@"没有录音设备");
break;
case WXVoiceRecoErrorOfCannotUseMic:
NSLog(@"无法使用mic");
break;
default:
break;
}
}
//录音完成,等待服务器返回识别结果。此时不会再接受新的语音
- (void)voiceInputWaitForResult{
}
//在手动调用的cancel后,取消完成时回调
- (void)voiceInputDidCancel{
}
4.点击开始按钮时,实现下面的方法
- (void)start:(UIButton *)button{
button.selected = !button.selected;
if (button.selected) {
self.messageLabel.text = @"正在识别中,请说话...";
[[WXVoiceSDK sharedWXVoice]startOnce];
[button setTitle:@"结束" forState:UIControlStateNormal];
}
else{
[[WXVoiceSDK sharedWXVoice]finish];
[button setTitle:@"开始" forState:UIControlStateNormal];
}
}
网友评论