标签:
1 #import "iflyMSC/IFlySpeechUtility.h"
1 //第二步:登陆科大讯飞语音平台 2 NSString *appID = [NSString stringWithFormat:@"appid=%@",@"570f0a8b"]; 3 [IFlySpeechUtility createUtility:appID];
1 #import "FirstViewController.h" 2 3 //第一步:引入库文件 4 //科大讯飞语音识别功能回调方法的接口文件 5 #import <iflyMSC/IFlyRecognizerViewDelegate.h> 6 //科大讯飞语音识别功能的声音识别视图 7 #import <iflyMSC/IFlyRecognizerView.h> 8 //科大讯飞语音识别功能中定义的常量 9 #import <iflyMSC/IFlySpeechConstant.h> 10 11 ///遵循代理协议 12 @interface FirstViewController ()<IFlyRecognizerViewDelegate> 13 14 ///语音识别对象 15 @property (nonatomic,strong)IFlyRecognizerView *iflyRecognizerView; 16 17 ///接收相关结果的字符串 18 @property (nonatomic,strong)NSMutableString *result; 19 20 ///展示识别内容的textView 21 @property (weak, nonatomic) IBOutlet UITextView *showContentTextView; 22 23 24 25 @end 26 27 @implementation FirstViewController 28 29 - (void)viewDidLoad { 30 [super viewDidLoad]; 31 // Do any additional setup after loading the view. 32 33 34 //创建声音识别视图对象,初始化声音识别控件 35 self.iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center]; 36 //delegate需要设置,确保delegate回调可以正常返回 37 self.iflyRecognizerView.delegate = self; 38 39 } 40 41 #pragma mark - 开始识别 42 - (IBAction)beginRecognise:(id)sender { 43 44 [self startListenning]; 45 46 } 47 48 - (void)startListenning 49 { 50 //设置语音识别结果应用为普通文本领域 51 [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]]; 52 //设置前端点检测时间为6000ms 53 [self.iflyRecognizerView setParameter: @"6000" forKey:[IFlySpeechConstant VAD_BOS]]; 54 //设置后端点检测时间为700ms 55 [self.iflyRecognizerView setParameter: @"700" forKey:[IFlySpeechConstant VAD_EOS]]; 56 //设置采样率为8000 57 [self.iflyRecognizerView setParameter: @"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; 58 //设置为返回结果中包含标点符号 59 [self.iflyRecognizerView setParameter: @"1" forKey:[IFlySpeechConstant ASR_PTT]]; 60 //设置语音识别完成后数据的返回数据结构类型xml 61 [self.iflyRecognizerView setParameter: @"plain" forKey:[IFlySpeechConstant RESULT_TYPE]]; 62 //设置在Documents文件夹下缓存的文件名为temp.asr 63 [self.iflyRecognizerView setParameter: @"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; 64 //设置自定义的参数 65 [self.iflyRecognizerView setParameter: @"custom" forKey:[IFlySpeechConstant PARAMS]]; 66 67 [self.iflyRecognizerView start]; 68 69 } 70 71 #pragma mark - 代理方法 72 //成功 73 - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{ 74 75 76 self.result = [[NSMutableString alloc] init]; 77 NSDictionary *dic = [resultArray objectAtIndex:0]; 78 79 for (NSString *key in dic) 80 { 81 [self.result appendFormat:@"%@",key]; 82 } 83 NSLog(@"%@---------",_result); 84 85 //自定义控件显示内容 86 self.showContentTextView.text = [NSString stringWithFormat:@"%@%@",self.showContentTextView.text,self.result]; 87 } 88 89 //失败 90 - (void)onError:(IFlySpeechError *)error{ 91 92 NSLog(@"%@",error); 93 }
1 #import "SecondViewController.h" 2 3 //第一步:引入头文件 4 //文字识别的回调方法接口 5 #import <iflyMSC/IFlySpeechSynthesizerDelegate.h> 6 //文字识别对象 7 #import <iflyMSC/IFlySpeechSynthesizer.h> 8 //科大讯飞语音框架定义的常量 9 #import <iflyMSC/IFlySpeechConstant.h> 10 11 @interface SecondViewController ()<IFlySpeechSynthesizerDelegate> 12 13 //文字识别对象 14 @property (strong, nonatomic) IFlySpeechSynthesizer *synthesizer; 15 16 17 18 ///输入内容的文本框 19 @property (weak, nonatomic) IBOutlet UITextView *inputContentTextView; 20 21 @end 22 23 @implementation SecondViewController 24 25 - (void)viewDidLoad { 26 [super viewDidLoad]; 27 // Do any additional setup after loading the view. 28 29 //创建文字识别对象 30 self.synthesizer = [IFlySpeechSynthesizer sharedInstance]; 31 32 //指定文字识别对象的代理对象 33 self.synthesizer.delegate = self; 34 35 //设置文字识别对象的关键属性 36 [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]]; 37 [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]]; 38 [self.synthesizer setParameter:@"XIAOYAN" forKey:[IFlySpeechConstant VOICE_NAME]]; 39 [self.synthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; 40 [self.synthesizer setParameter:@"temp.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]]; 41 [self.synthesizer setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]]; 42 43 } 44 45 #pragma mark - 识别相关的内容 46 - (IBAction)beginRecognise:(id)sender { 47 48 [self.synthesizer startSpeaking:@"最后一节课了,大家以后要加油"]; 49 50 } 51 52 #pragma mark - 代理方法 53 - (void)onCompleted:(IFlySpeechError *)error{ 54 55 NSLog(@"%@",error); 56 }
标签:
原文地址:http://www.cnblogs.com/leikun1113/p/5557000.html