iOS_科大讯飞快速实现语音搜索功能Demo
配置工程看开发文档就行了,本Demo就是创建一个按钮和显示语音搜索结果的文本框:直接上代码:
#import "ViewController.h" #import <iflyMSC/iflyMSC.h> @interface ViewController ()<IFlyRecognizerViewDelegate> @property (nonatomic, strong) IFlyRecognizerView *iFlyRecognizerView; @property (nonatomic, strong) NSString *result;/**< 显示结果的字符串 */ @property (nonatomic, strong) UIButton *button;/**< 点击开始的录音按钮 */ @property (nonatomic, assign) BOOL isStart; /**< 是否录音的状态 */ @property (nonatomic, strong) UILabel *titleLabel; /**< 显示语音结果的lable */ @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.isStart = NO; [self createSubViews]; /** 初始化语音识别空间 */ //1.创建语音听写对象 self.iFlyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center]; //2.设置听写参数 [_iFlyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]]; //asr_audio_path是录音文件名,设置value为nil或者为空取消保存,默认保存目录在 Library/cache下。 [_iFlyRecognizerView setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; } - (void)createSubViews { self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 100)]; _titleLabel.backgroundColor = [UIColor cyanColor]; _titleLabel.numberOfLines = 0; _titleLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_titleLabel]; self.button = [UIButton buttonWithType:UIButtonTypeCustom]; _button.frame = CGRectMake((self.view.frame.size.width - 80)/2, 200, 80, 30); _button.backgroundColor = [UIColor redColor]; [_button setTitle:@"语音收索" forState:UIControlStateNormal]; _button.showsTouchWhenHighlighted = YES; [self.view addSubview:_button]; [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; } - (void)buttonAction:(UIButton *)button { _iFlyRecognizerView.delegate = self; [self.view addSubview:_iFlyRecognizerView]; BOOL ret = [_iFlyRecognizerView start]; if (ret) { _isStart = YES; } else { _isStart = NO; } } /** * 识别返回结果代理 @param resultArray 识别结果 @ param isLast 表示是否最后一次结果 */ - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast { NSMutableString *resultString = [[NSMutableString alloc] init]; NSDictionary *dic = resultArray[0]; for (NSString *key in dic) { [resultString appendFormat:@"%@",key]; } NSLog(@"语音识别结果:%@", resultString); NSData *data = [resultString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"打印字典:%@", resultDic); NSArray *array_ws = [resultDic objectForKey:@"ws"]; // 遍历单词 NSMutableString *resultStr = [[NSMutableString alloc] init]; for (int i = 0; i < array_ws.count; i++) { NSMutableArray *temp = [[array_ws objectAtIndex:i] objectForKey:@"cw"]; NSDictionary * dic_cw = [temp objectAtIndex:0]; //[resultStr stringByAppendingString:[dic_cw objectForKey:@"w"]]; [resultStr appendString:[dic_cw objectForKey:@"w"]]; NSLog(@"识别结果:%@",[dic_cw objectForKey:@"w"]); } NSLog(@"最终识别结果@@@@@@@@@@@@:%@", resultStr); // 去掉识别结果的标点符号 if ([resultStr isEqualToString:@"。"] || [resultStr isEqualToString:@"?"] || [resultStr isEqualToString:@"!"]) { NSLog(@"标点符号%@", resultStr); } else { _titleLabel.text = resultStr; } } -(void)onError:(IFlySpeechError *)error { } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }