注:添加 iflyMSC.framework 时,请注意工程 BuildSetting中的framwork path的设置,如果出现找不到framework的情况,可以将path清空,在 xcode 中删除 framework,然后重新添加。
如果使用了离线识别,还需要增加 libc++.dylib。
//
// ViewController.h
// VoiceIdentify
//
// Created by 涂婉丽 on 16/1/22.
// Copyright © 2016年 涂婉丽. All rights reserved.
//
#import
#import "iflyMSC/iflyMSC.h"
@class IFlyDataUploader;
@class IFlySpeechRecognizer;
@interface ViewController : UIViewController
@property (nonatomic, strong) NSString *pcmFilePath;//音频文件路径
@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;//不带界面的识别对象
@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//带界面的识别对象
@property (nonatomic, strong) IFlyDataUploader *uploader;//数据上传对象
- (IBAction)resignizeAction;
@property (unsafe_unretained, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) NSString * result;
@end
// ViewController.m
// VoiceIdentify
//
// Created by 涂婉丽 on 16/1/22.
// Copyright © 2016年 涂婉丽. All rights reserved.
//
#import "ViewController.h"
#import
#import "ISRDataHelper.h"
#define APPID_VALUE @"56a1b495"
#define TIMEOUT_VALUE @"20000"
#import "iflyMSC/IFlyMSC.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.uploader = [[IFlyDataUploader alloc] init];
//demo录音文件保存路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
_pcmFilePath = [[NSString alloc] initWithFormat:@"%@",[cachePath stringByAppendingPathComponent:@"asr.pcm"]];
_textView.clipsToBounds = YES;
_textView.layer.cornerRadius = 5;
_textView.layer.borderColor=[UIColor lightGrayColor].CGColor;
_textView.layer.borderWidth = 0.5;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self initRecognizer ];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)resignizeAction {
// if(_iflyRecognizerView == nil)
// {
// [self initRecognizer ];
// }
[_textView setText:@""];
[_textView resignFirstResponder];
//设置音频来源为麦克风
[_iflyRecognizerView setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];
//设置听写结果格式为json
[_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
//保存录音文件,保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下
[_iflyRecognizerView setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
[_iflyRecognizerView start];
}
- (void)initRecognizer
{
NSLog(@"%s",__func__);
_iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
_iflyRecognizerView.delegate = self;
[_iflyRecognizerView setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
//asr_audio_path保存录音文件名,如不再需要,设置value为nil表示取消,默认目录是documents
[_iflyRecognizerView setParameter:@"asrview.pcm " forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
_iflyRecognizerView.delegate = self;
//设置最长录音时间
[_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//设置后端点
[_iflyRecognizerView setParameter:@"3000" forKey:[IFlySpeechConstant VAD_EOS]];
//设置前端点
[_iflyRecognizerView setParameter:@"3000" forKey:[IFlySpeechConstant VAD_BOS]];
//网络等待时间
[_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
//设置采样率,推荐使用16K
[_iflyRecognizerView setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
//设置语言
[_iflyRecognizerView setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
//设置是否返回标点符号
[_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
}
/**
无界面,听写结果回调
results:听写结果
isLast:表示最后一次
****/
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
NSMutableString *resultString = [[NSMutableString alloc] init];
NSDictionary *dic = results[0];
for (NSString *key in dic) {
[resultString appendFormat:@"%@",key];
}
_result =[NSString stringWithFormat:@"%@%@", _textView.text,resultString];
NSString * resultFromJson = [ISRDataHelper stringFromJson:resultString];
_textView.text = [NSString stringWithFormat:@"%@%@", _textView.text,resultFromJson];
if (isLast){
NSLog(@"听写结果(json):%@测试", self.result);
}
NSLog(@"_result=%@",_result);
NSLog(@"resultFromJson=%@",resultFromJson);
NSLog(@"isLast=%d,_textView.text=%@",isLast,_textView.text);
}
/**
有界面,听写结果回调
resultArray:听写结果
isLast:表示最后一次
****/
- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
{
NSLog(@"---------------%@",resultArray);
NSLog(@"bbbbbbbbbbbbb%d",isLast);
NSMutableString *result = [[NSMutableString alloc] init];
NSDictionary *dic = [resultArray objectAtIndex:0];
NSLog(@"==============%@",dic);
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
}
_textView.text = [NSString stringWithFormat:@"%@%@",_textView.text,result];
}
/**
开始识别回调
****/
- (void) onBeginOfSpeech
{
NSLog(@"onBeginOfSpeech");
}
/**
停止录音回调
****/
- (void) onEndOfSpeech
{
NSLog(@"onEndOfSpeech");
}
/**
听写结束回调(注:无论听写是否正确都会回调)
error.errorCode =
0 听写正确
other 听写出错
****/
- (void) onError:(IFlySpeechError *) error
{
NSLog(@"%s",__func__);
NSLog(@"errorCode:%d",[error errorCode]);
}
@end
#import
@interface ISRDataHelper : NSObject
// 解析命令词返回的结果
+ (NSString*)stringFromAsr:(NSString*)params;
/**
解析JSON数据
****/
+ (NSString *)stringFromJson:(NSString*)params;//
/**
解析语法识别返回的结果
****/
+ (NSString *)stringFromABNFJson:(NSString*)params;
@end
#import "ISRDataHelper.h"
@implementation ISRDataHelper
/**
解析命令词返回的结果
****/
+ (NSString*)stringFromAsr:(NSString*)params;
{
NSMutableString * resultString = [[NSMutableString alloc]init];
NSString *inputString = nil;
NSArray *array = [params componentsSeparatedByString:@"\n"];
for (int index = 0; index{
NSRange range;
NSString *line = [array objectAtIndex:index];
NSRange idRange = [line rangeOfString:@"id="];
NSRange nameRange = [line rangeOfString:@"name="];
NSRange cOnfidenceRange= [line rangeOfString:@"cOnfidence="];
NSRange grammarRange = [line rangeOfString:@" grammar="];
NSRange inputRange = [line rangeOfString:@"input="];
if (confidenceRange.length == 0 || grammarRange.length == 0 || inputRange.length == 0 )
{
continue;
}
//check nomatch
if (idRange.length!=0) {
NSUInteger idPosX = idRange.location + idRange.length;
NSUInteger idLength = nameRange.location - idPosX;
range = NSMakeRange(idPosX,idLength);
NSString *idValue = [[line substringWithRange:range]
stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet] ];
if ([idValue isEqualToString:@"nomatch"]) {
return @"";
}
}
//Get Confidence Value
NSUInteger cOnfidencePosX= confidenceRange.location + confidenceRange.length;
NSUInteger cOnfidenceLength= grammarRange.location - confidencePosX;
range = NSMakeRange(confidencePosX,confidenceLength);
NSString *score = [line substringWithRange:range];
NSUInteger inputStringPosX = inputRange.location + inputRange.length;
NSUInteger inputStringLength = line.length - inputStringPosX;
range = NSMakeRange(inputStringPosX , inputStringLength);
inputString = [line substringWithRange:range];
[resultString appendFormat:@"%@ 置信度%@\n",inputString, score];
}
return resultString;
}
/**
解析听写json格式的数据
params例如:
{"sn":1,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"w":"白日","sc":0}]},{"bg":0,"cw":[{"w":"依山","sc":0}]},{"bg":0,"cw":[{"w":"尽","sc":0}]},{"bg":0,"cw":[{"w":"黄河入海流","sc":0}]},{"bg":0,"cw":[{"w":"。","sc":0}]}]}
****/
+ (NSString *)stringFromJson:(NSString*)params
{
if (params == NULL) {
return nil;
}
NSMutableString *tempStr = [[NSMutableString alloc] init];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData: //返回的格式必须为utf8的,否则发生未知错误
[params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
if (resultDic!= nil) {
NSArray *wordArray = [resultDic objectForKey:@"ws"];
for (int i = 0; i <[wordArray count]; i++) {
NSDictionary *wsDic = [wordArray objectAtIndex: i];
NSArray *cwArray = [wsDic objectForKey:@"cw"];
for (int j = 0; j <[cwArray count]; j++) {
NSDictionary *wDic = [cwArray objectAtIndex:j];
NSString *str = [wDic objectForKey:@"w"];
[tempStr appendString: str];
}
}
}
return tempStr;
}
/**
解析语法识别返回的结果
****/
+ (NSString *)stringFromABNFJson:(NSString*)params
{
if (params == NULL) {
return nil;
}
NSMutableString *tempStr = [[NSMutableString alloc] init];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:
[params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSArray *wordArray = [resultDic objectForKey:@"ws"];
for (int i = 0; i <[wordArray count]; i++) {
NSDictionary *wsDic = [wordArray objectAtIndex: i];
NSArray *cwArray = [wsDic objectForKey:@"cw"];
for (int j = 0; j <[cwArray count]; j++) {
NSDictionary *wDic = [cwArray objectAtIndex:j];
NSString *str = [wDic objectForKey:@"w"];
NSString *score = [wDic objectForKey:@"sc"];
[tempStr appendString: str];
[tempStr appendFormat:@" 置信度:%@",score];
[tempStr appendString: @"\n"];
}
}
return tempStr;
}
@end