上次简单地讲解了如何利用科大讯飞完成语音合成,今天接着也把语音识别整理一下。当然,写代码前我们需要做的一些工作(如申请appid、导库),在上一篇语音合成的文章当中已经说过了,不了解的可以看看我上次的博文,那么这次直接从堆代码开始吧。
详细步骤:
导完类库之后,在工程里添加好用的头文件。在视图里只用了一个UITextField显示识别的内容,两个UIButton(一个开始监听语音,一个结束监听),然后引入类、添加代理,和语音合成的一样。
在ViewController.h 中代码如下:
#import <UIKit/UIKit.h>
#import "iflyMSC/IFlySpeechRecognizerDelegate.h"
#import "iflyMSC/iflyMSC.h"
//引入语音识别类
@class IFlyDataUploader;
@class IFlySpeechUnderstander;
//注意要添加语音识别代理
@interface OtherViewController : UIViewController<IFlySpeechRecognizerDelegate>
@property (nonatomic,strong) IFlySpeechUnderstander *iFlySpeechUnderstander;
@property (weak, nonatomic) IBOutlet UITextView *content;
@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//带界面的识别对象
@property (nonatomic,strong) NSString *result;
@property (nonatomic,strong) NSString *str_result;
@property (nonatomic) BOOL isCanceled;
- (IBAction)understand:(id)sender;
- (IBAction)finish:(id)sender;
@end
在ViewController.m 中代码如下:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import "iflyMSC/IFlyContact.h"
#import "iflyMSC/IFlyDataUploader.h"
#import "iflyMSC/IFlyUserWords.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "iflyMSC/IFlySpeechUnderstander.h"
@interface OtherViewController ()
@end
@implementation OtherViewController
-(void)viewWillDisappear:(BOOL)animated
{
[_iFlySpeechUnderstander cancel];
_iFlySpeechUnderstander.delegate = nil;
//设置回非语义识别
[_iFlySpeechUnderstander destroy];
[super viewWillDisappear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
_content.layer.borderColor = [[UIColor colorWithRed:230.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0]CGColor];
_content.layer.borderWidth = 3.0;
_content.layer.cornerRadius = 8.0f;
[_content.layer setMasksToBounds:YES];
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"562d9d5d",@"20000"];
//所有服务启动前,需要确保执行createUtility
[IFlySpeechUtility createUtility:initString];
_iFlySpeechUnderstander = [IFlySpeechUnderstander sharedInstance];
_iFlySpeechUnderstander.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)understand:(id)sender {
bool ret = [_iFlySpeechUnderstander startListening]; //开始监听
if (ret) {
self.isCanceled = NO;
}
else{
NSLog(@"启动识别失败!");
}
}
- (IBAction)finish:(id)sender {
[_iFlySpeechUnderstander stopListening]; //结束监听,并开始识别
}
#pragma mark - IFlySpeechRecognizerDelegate
/**
* @fn onVolumeChanged
* @brief 音量变化回调
* @param volume -[in] 录音的音量,音量范围1~100
* @see
*/
- (void) onVolumeChanged: (int)volume
{
// NSString * vol = [NSString stringWithFormat:@"音量:%d",volume];
// NSLog(@"%@",vol);
}
/**
* @fn onBeginOfSpeech
* @brief 开始识别回调
* @see
*/
- (void) onBeginOfSpeech
{
}
/**
* @fn onEndOfSpeech
* @brief 停止录音回调
* @see
*/
- (void) onEndOfSpeech
{
}
/**
* @fn onError
* @brief 识别结束回调
* @param errorCode -[out] 错误类,具体用法见IFlySpeechError
*/
- (void) onError:(IFlySpeechError *)errorCode
{
NSString *text ;
if (self.isCanceled) {
text = @"识别取消";
}
else if (errorCode ==0 ) {
if (_result.length==0) {
text = @"无识别结果";
}
else{
text = @"识别成功";
}
}
else{
text = [NSString stringWithFormat:@"发生错误:%@",errorCode];
NSLog(@"%@",text);
}
}
/**
* @fn onResults
* @brief 识别结果回调
* @param result -[out] 识别结果,NSArray的第一个元素为NSDictionary,NSDictionary的key为识别结果,value为置信度
* @see
*/
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
NSArray * temp = [[NSArray alloc]init];
NSString * str = [[NSString alloc]init];
NSMutableString *result = [[NSMutableString alloc] init];
NSDictionary *dic = results[0];
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
}
NSLog(@"听写结果:%@",result);
//---------讯飞语音识别JSON数据解析---------//
NSError * error;
NSData * data = [result dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"data: %@",data);
NSDictionary * dic_result =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray * array_ws = [dic_result objectForKey:@"ws"];
//遍历识别结果的每一个单词
for (int i=0; i<array_ws.count; i++) {
temp = [[array_ws objectAtIndex:i] objectForKey:@"cw"];
NSDictionary * dic_cw = [temp objectAtIndex:0];
str = [str stringByAppendingString:[dic_cw objectForKey:@"w"]];
NSLog(@"识别结果:%@",[dic_cw objectForKey:@"w"]);
}
NSLog(@"最终的识别结果:%@",str);
//去掉识别结果最后的标点符号
if ([str isEqualToString:@"。"] || [str isEqualToString:@"?"] || [str isEqualToString:@"!"]) {
NSLog(@"末尾标点符号:%@",str);
}
else{
self.content.text = str;
}
_result = str;
}
@end