1、首先下载讯飞sdk及文档:http://open.voicecloud.cn/
2、学习里面的demo简单实现了一个小的语音识别功能
先做一个简单demo,看看识别效果。注:语音识别必须联网。
所有接口必需在联网状态下才能正常使用。
效果图:

1#import <UIKit/UIKit.h> 2#import "iflyMSC/IFlySpeechRecognizer.h" 3#import "iflyMSC/IFlyDataUploader.h" 4 5@protocol SpeechAlertViewDelegate <NSObject> 6@optional 7- (void)getResultText:(NSString *)text; 8@end 9 10@interface SpeechAlertView : UIAlertView<IFlySpeechRecognizerDelegate> 11{ 12 UIImageView *speechImage;//声音图片 13 14 IFlySpeechRecognizer * _iFlySpeechRecognizer;//语音识别对象 15 UIView *backgroundView; 16} 17@property (assign, nonatomic)id<SpeechAlertViewDelegate> speechDelegate; 18@end 19 20 21 22#import "SpeechAlertView.h" 23#define APPID @"51de5743" 24#define TIMEOUT @"20000" 25// timeout 连接超时的时间,以ms为单位,毫秒,符号ms ,1000 毫秒 = 1秒,30000=30秒 26//timeout:网络超时时间,单位:ms,默认为20000,范围0-30000 27@implementation SpeechAlertView 28 29-(id)init 30{ 31 self = [super initWithFrame:CGRectMake(0, 0, 300, 220)]; 32 if (self) { 33 // Initialization code 34 } 35 return self; 36} 37- (id)initWithFrame:(CGRect)frame 38{ 39 self = [super initWithFrame:frame]; 40 if (self) { 41 // Initialization code 42 } 43 return self; 44} 45 46//uialertview的大小位置 47-(void)setFrame:(CGRect)frame{ 48 //重新设置弹出框的大小和位置 49 UIWindow *window = [UIApplication sharedApplication].keyWindow; 50 51 [super setFrame:CGRectMake((window.frame.size.width-self.frame.size.width)/2, (window.frame.size.height-self.frame.size.height)/2, self.frame.size.width, self.frame.size.height)]; 52} 53//重新写界面内容 54- (void) layoutSubviews { 55 //屏蔽系统的ImageView 和 UIButton 56 for (UIView *v in [self subviews]) { 57 if ([v class] == [UIImageView class]){ 58 [v setHidden:YES]; 59 } 60 61 62 if ([v isKindOfClass:[UIButton class]] || 63 [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) { 64 [v setHidden:YES]; 65 } 66 } 67 68 //添加背影图 69 UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 70 backView.backgroundColor = [UIColor colorWithRed:66/255.0 green:68/255.0 blue:70/255.0 alpha:1.0]; 71 [self addSubview:backView]; 72 73 //添加标题 74 UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, backView.frame.size.width-20, 30)]; 75 titleLabel.backgroundColor = [UIColor clearColor]; 76 titleLabel.text = @"语音识别"; 77 titleLabel.font = [UIFont systemFontOfSize:16]; 78 titleLabel.textColor = [UIColor colorWithRed:218.0/255.0 green:217.0/255.0 blue:217.0/255.0 alpha:1]; 79 [backView addSubview:titleLabel]; 80 81 //添加关闭按钮huati_close 82 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 83 [button setImage:[UIImage imageNamed:@"alert_close.png"] forState:UIControlStateNormal]; 84 [backView addSubview:button]; 85 button.tag = 1; 86 button.frame = CGRectMake(backView.frame.size.width-25, 5, 20, 20); 87 [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 88 89 //添加黄线 90 UIView *xianView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, backView.frame.size.width, 1)]; 91 xianView.backgroundColor = [UIColor yellowColor]; 92 [backView addSubview:xianView]; 93 94 //添加内容 95 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, backView.frame.size.width, 40)]; 96 label.backgroundColor = [UIColor clearColor]; 97 label.text = @"默认不讲话5秒后自动关闭,间隔不讲话2秒后关闭,最多说20秒"; 98 label.font = [UIFont boldSystemFontOfSize:15]; 99 label.textAlignment = UITextAlignmentCenter; 100 label.textColor = [UIColor yellowColor]; 101 [backView addSubview:label]; 102 label.numberOfLines = 0; 103 104 //添加中间图片 105 speechImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-50)/2, 80, 50, 85)]; 106 speechImage.image = [UIImage imageNamed:@"yuyin_01.png"]; 107 [backView addSubview:speechImage]; 108 109 //添加说完了按钮 110 UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom]; 111 112 submitButton.frame = CGRectMake((backView.frame.size.width-170)/2, 170, 150, 35); 113 submitButton.tag = 2; 114 [submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 115 [submitButton setTitle:@"说完了" forState:UIControlStateNormal]; 116 [submitButton setBackgroundImage:[UIImage imageNamed:@"alert_tButton.png"] forState:UIControlStateNormal]; 117 [submitButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 118 [backView addSubview:submitButton]; 119 //想添加什么由此添加 120 121 //创建对象 122 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",APPID,TIMEOUT]; 123 //语音识别对象创建 124 _iFlySpeechRecognizer = nil; 125 _iFlySpeechRecognizer = [IFlySpeechRecognizer createRecognizer:initString delegate:self]; 126// _iFlySpeechRecognizer.delegate = self; 127 /* 128 2.vad_bos:静音超时时间,即用户多长时间不说话则当做超 时处理,单位:ms,engine 指定 sms 识别默认值为 5000,其他 情况默认值为 4000,范围 0-10000; 129 3.vad_eos:后端点静音检测时间,即用户停止说话多长时间 内即认为不再输入,自动停止录音,单位:ms,sms 识别默认 值为 1800,其他默认值为 700,范围 0-10000; 130 */ 131 [_iFlySpeechRecognizer setParameter:@"domain" value:@"sms"]; 132 [_iFlySpeechRecognizer setParameter:@"sample_rate" value:@"16000"]; 133 [_iFlySpeechRecognizer setParameter:@"plain_result" value:@"0"]; 134 initString = nil; 135 136 //开始识别 137 [_iFlySpeechRecognizer startListening]; 138 139} 140//按钮处理方法 141-(void) buttonClicked:(id)sender 142{ 143 [self dismissWithClickedButtonIndex:0 animated:YES]; 144 145} 146 147//显示 148-(void)show 149{ 150// [super show]; 151 UIWindow *window = [UIApplication sharedApplication].keyWindow; 152 backgroundView = [[UIView alloc]initWithFrame:window.frame]; 153 backgroundView.backgroundColor = [UIColor clearColor]; 154 [backgroundView addSubview:self]; 155 [window addSubview:backgroundView]; 156} 157//弹出框消失 158-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 159{ 160 [_iFlySpeechRecognizer stopListening]; 161 [_iFlySpeechRecognizer cancel]; 162 [_iFlySpeechRecognizer setDelegate:nil]; 163 _iFlySpeechRecognizer = nil; 164 speechImage = nil; 165 [backgroundView removeFromSuperview]; 166 backgroundView = nil; 167} 168 169 170#pragma mark - IFlySpeechRecognizerDelegate 171- (void) onVolumeChanged: (int)volume 172{ 173 NSLog(@"%d",volume); 174 //录音的音量,音量范围1~100 175 if (volume>=0 &&volume<=5) { 176 speechImage.image = [UIImage imageNamed:@"yuyin_01.png"]; 177 }else if(volume>5 && volume<=30){ 178 speechImage.image = [UIImage imageNamed:@"yuyin_02.png"]; 179 }else{ 180 speechImage.image = [UIImage imageNamed:@"yuyin_03.png"]; 181 } 182} 183 184- (void) onBeginOfSpeech 185{ 186 NSLog(@"正在录音"); 187} 188 189- (void) onEndOfSpeech 190{ 191 NSLog(@"停止录音"); 192} 193 194- (void) onError:(IFlySpeechError *) error 195{ 196 NSLog(@"停止录音%@,%@",error,[error errorDesc]); 197 [self dismissWithClickedButtonIndex:0 animated:YES]; 198} 199 200//结果 201- (void) onResults:(NSArray *) results 202{ 203 NSMutableString *result = [[NSMutableString alloc] init]; 204 NSDictionary *dic = [results objectAtIndex:0]; 205 for (NSString *key in dic) { 206 [result appendFormat:@"%@",key]; 207 } 208 NSLog(@"转写结果:%@--results:%@",result,results); 209 210 //返回结果 211 [_speechDelegate getResultText:result]; 212} 213 214@end
源码下载地址: