iphone之使用讯飞语音sdk实现语音识别功能

来源:互联网 发布:linux停止tomcat服务 编辑:程序博客网 时间:2024/05/29 14:24

1、首先下载讯飞sdk及文档:http://open.voicecloud.cn/

2、学习里面的demo简单实现了一个小的语音识别功能

先做一个简单demo,看看识别效果。注:语音识别必须联网。

所有接口必需在联网状态下才能正常使用。

效果图:


[cpp] view plaincopy
  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  

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

源码下载地址:

http://download.csdn.net/detail/rhljiayou/5889565

0 0
原创粉丝点击