5 实现代理方式
讯飞对识别结果的处理运用的代理回调的方式,实现IFlySpeechSynthesizerDelegate协议的onResult:isLast:方法.
注意!!!!这里的是onResults,不是onResult,前者是有图标提示的语音识别的结果回调函数.
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{ NSMutableString *result = [[NSMutableString alloc] init]; NSDictionary *dic = [results objectAtIndex:0]; for (NSString *key in dic) { [result appendFormat:@"%@",key]; } NSString * resu = [ISRDataHelper stringFromJson:result]; _text.text = [NSString stringWithFormat:@"%@%@",_text.text,resu]; }
6 触发语音合成

添加一个输入框,一个button,button的响应时间是将输入框中的文本内容读出来.
if(_iFlySpeechRecognizer == nil) { [self initRecognizer]; } [_iFlySpeechRecognizer cancel]; //设置音频来源为麦克风 [_iFlySpeechRecognizer setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"]; //设置听写结果格式为json [_iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]]; //保存录音文件,保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下 [_iFlySpeechRecognizer setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]]; [_iFlySpeechRecognizer setDelegate:self]; BOOL ret = [_iFlySpeechRecognizer startListening];
语音合成和语音识别的过程差不多
1 导入头文件
//将讯飞SDK中的所有类都导入进来 #import < iflyMSC/iflyMSC.h > #import "PcmPlayer.h" #import "TTSConfig.h"
2 登陆讯飞服务器
在使用讯飞的语音解析之前,需要进行用户身份验证,即登陆讯飞服务器,这个在viewDidLoad()方法中添加两行代码即可.后面的ID数字就是之前我们在开放系统建立按自己应用时帮的APPID,在下载的SDK中也有有的.
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"565e4dd9"]; [IFlySpeechUtility createUtility:initString];
3 创建有图标提示的语音识别对象
创建一个讯飞语音识别对象ifly文件用什么打开,可以对他进行一系列的调用
typedef NS_OPTIONS(NSInteger, SynthesizeType) { NomalType = 5,//普通合成 UriType = 6, //uri合成 }; @property (nonatomic, strong) IFlySpeechSynthesizer * iFlySpeechSynthesizer;//语音合成对象 @property (nonatomic, strong) PcmPlayer *audioPlayer;//用于播放音频的 @property (nonatomic, assign) SynthesizeType synType;//是何种合成方式 @property (nonatomic, assign) BOOL hasError;//解析过程中是否出现错误
4 对象初始化
前面声明了一些在语音合成的之后会用到的对象,现在必须对上面的对象进行初始化.还是在ViewDidLoad()里面进行就可以了.
TTSConfig *instance = [TTSConfig sharedInstance]; if (instance == nil) { return; } //合成服务单例 if (_iFlySpeechSynthesizer == nil) { _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance]; } _iFlySpeechSynthesizer.delegate = self; //设置语速1-100 [_iFlySpeechSynthesizer setParameter:instance.speed forKey:[IFlySpeechConstant SPEED]]; //设置音量1-100 [_iFlySpeechSynthesizer setParameter:instance.volume forKey:[IFlySpeechConstant VOLUME]]; //设置音调1-100 [_iFlySpeechSynthesizer setParameter:instance.pitch forKey:[IFlySpeechConstant PITCH]]; //设置采样率 [_iFlySpeechSynthesizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]]; //设置发音人 [_iFlySpeechSynthesizer setParameter:instance.vcnName forKey:[IFlySpeechConstant VOICE_NAME]];
5 触发语音合成
添加一个输入框,一个button,button的响应时间是将输入框中的文本内容读出来.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-136472-2.html
全国各地
苏-30