标签:
#import "AppDelegate.h" #import "iflyMSC/IFlySpeechUtility.h"//
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 //登录科大讯飞语音平台 5 NSString *appID = [NSString stringWithFormat:@"appid=%@",@"570f0a8b"]; 6 [IFlySpeechUtility createUtility:appID]; 7 8 return YES; 9 }
#import "FirstViewController.h" //第一步:引入库文件 //科大讯飞语音识别功能回调方法的接口文件 #import <iflyMSC/IFlyRecognizerViewDelegate.h> //科大讯飞语音识别功能的声音识别视图 #import <iflyMSC/IFlyRecognizerView.h> //科大讯飞语音识别功能中定义的常量 #import <iflyMSC/IFlySpeechConstant.h> //遵循代理协议 @interface FirstViewController ()<IFlyRecognizerViewDelegate> //语音识别对象 @property(nonatomic,strong)IFlyRecognizerView *iflyRecognizerView; //接受相关结果的字符串 @property(nonatomic,strong)NSMutableString *result; //展示识别内容TextView @property (weak, nonatomic) IBOutlet UITextView *showContentTextView; @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //创建声音识别视图对象,初始化声音识别控件 self.iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center]; //delegate需要设置,确保delegate回调可以正常返回 self.iflyRecognizerView.delegate = self; } #pragma mark - 开始识别 - (IBAction)beginRecognise:(id)sender { [self startListenning]; } - (void)startListenning { //设置语音识别结果应用为普通文本领域 [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]]; //设置前端点检测时间为6000ms [self.iflyRecognizerView setParameter: @"6000" forKey:[IFlySpeechConstant VAD_BOS]]; //设置后端点检测时间为700ms [self.iflyRecognizerView setParameter: @"700" forKey:[IFlySpeechConstant VAD_EOS]]; //设置采样率为8000 [self.iflyRecognizerView setParameter: @"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; //设置为返回结果中包含标点符号 [self.iflyRecognizerView setParameter: @"1" forKey:[IFlySpeechConstant ASR_PTT]]; //设置语音识别完成后数据的返回数据结构类型xml [self.iflyRecognizerView setParameter: @"plain" forKey:[IFlySpeechConstant RESULT_TYPE]]; //设置在Documents文件夹下缓存的文件名为temp.asr [self.iflyRecognizerView setParameter: @"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; //设置自定义的参数 [self.iflyRecognizerView setParameter: @"custom" forKey:[IFlySpeechConstant PARAMS]]; [self.iflyRecognizerView start]; }
#pragma mark - 代理方法 //成功 -(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{ self.result = [[NSMutableString alloc] init]; NSDictionary *dic = [resultArray objectAtIndex:0]; for (NSString *key in dic) { [self.result appendFormat:@"%@",key]; } NSLog(@"%@---------",_result); //自定义控件显示内容 self.showContentTextView.text = [NSString stringWithFormat:@"%@%@",self.showContentTextView.text,self.result]; } //失败 -(void)onError:(IFlySpeechError *)error{ NSLog(@"error = %@",error); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
整体流程跟上面一样下面来写一下代码
#import "SecondViewController.h" //第一步:引入头文件 //文字识别的回调方法接口 #import <iflyMSC/IFlySpeechSynthesizerDelegate.h> //文字识别对象 #import <iflyMSC/IFlySpeechSynthesizer.h> //科大讯飞语音框架定义的常量 #import <iflyMSC/IFlySpeechConstant.h> @interface SecondViewController ()<IFlySpeechSynthesizerDelegate>//引入协议 @property (weak, nonatomic) IBOutlet UITextField *input;//接收框属性 //创建文字识别对象 @property (strong, nonatomic) IFlySpeechSynthesizer *synthesizer; @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //创建文字识别对象 self.synthesizer = [IFlySpeechSynthesizer sharedInstance]; //指定文字识别对象的代理对象 self.synthesizer.delegate = self; //设置文字识别对象的关键属性 [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]]; [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]]; [self.synthesizer setParameter:@"XIAOYAN" forKey:[IFlySpeechConstant VOICE_NAME]]; [self.synthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; [self.synthesizer setParameter:@"temp.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]]; [self.synthesizer setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]]; } #pragma mark - 识别相关的内容 - (IBAction)beginRecgnise:(id)sender { [self.synthesizer startSpeaking:_input.text]; } #pragma mark - 代理方法 -(void)onCompleted:(IFlySpeechError *)error{ }
标签:
原文地址:http://www.cnblogs.com/manmq/p/5557400.html