科大讯飞语音集成(语音识别和语音合成)

讯飞开发步骤

1.
4.集成步骤

4.1、在创建应用的界面完成相关信息的填写,然后下载对应的SDK;
4.2、下载下来的SDK包解压出来有三个文件夹,第一个市doc文件夹,不多说,肯定是开发文档, 还有相关的作者信息什么的.最重要的是剩下的两个文件夹,一个是lib文件夹,这里存放的是讯飞SDK的类库文件,我们导入SDK就是导入的这里的文件. 第三个文件夹是一个IOS的Demo演示工程.
4.3、新建一个IOS工程,将lib文件夹下的”iflyMSC.framework”文件拷贝到工程目录下,然后在工程配置文件的[Build Phases]-[Link Binary With Libraries]中通过[AddOther];
4.4、确认SDK路径。在配置文件中搜索”head”找到[Framework Search Paths],点开查看SDK的路径是不是绝对路径,如果是如下图的样子,那就没问题了.这一步主要是确保SDK的路径为相对路径,防止工程换了文件夹位置就无法运行的情况的发生.
4.5、添加框架

5.语音识别

语音识别分两种,分别用在不同的场合,一个是界面提示的语音识别,一个是无界面提示的语音识别,这里以有界面提示的语音识别为例子.
5.1、有界面提示的语音识别
1 导入头文件
//将讯飞SDK中的所有类都导入进来
#import < iflyMSC/iflyMSC.h >
2 登陆讯飞服务器在使用讯飞的语音解析之前,需要进行用户身份验证,即登陆讯飞服务器,这个在viewDidLoad()方法中添加两行代码即可.后面的ID数字就是之前我们在开放平台创建按自己应用时给的APPID,在下载的SDK中也是有的.

NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"565e4dd9"];
[IFlySpeechUtility createUtility:initString];

3 创建有界面提示的语音识别对象创建一个讯飞语音识别对象,可以对他进行一系列的调用

@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//带界面的识别对象

4 初始化带界面的识别对象前面声明了一个带界面的语音识别对象,现在需要对这个识别对象进行初始化,同样是在ViewDidLoad()方法中进行就行.

#pragma mark ------ 进行有界面的语音识别的初始化
 _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
 _iflyRecognizerView.delegate = self;
 [_iflyRecognizerView setParameter: @“iat” forKey: [IFlySpeechConstant IFLY_DOMAIN]];
 //asr_audio_path保存录音文件名,如不再需要,设置value为nil表示取消,默认目录是documents
[_iflyRecognizerView setParameter:@"asrview.pcm " forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];

5 实现代理方法讯飞对识别结果的处理采用的代理回调的方法,实现IFlySpeechSynthesizerDelegate协议的onResult:isLast:方法.注意!!!这里的是onResult,不是onResults,后者是无界面提示的语音解析的结果回调函数.

- (void)onResult: (NSArray *)resultArray isLast:(BOOL) isLast
 {
 NSMutableString *result = [[NSMutableString alloc] init];
 NSDictionary *dic = [resultArray objectAtIndex:0];
 for (NSString *key in dic) {
 [result appendFormat:@"%@",key];
 }
 NSString * resu = [ISRDataHelper stringFromJson:result];
 //将结果显示在界面的Label上
 _text.text = [NSString stringWithFormat:@"%@%@",_text.text,resu];
 }


这里默认的传回来的是Json字符串,需要对字符串进行解析.当然,讯飞还是比较良心的,在demo中给提供了一个解析类,就是上面用到的ISRDataHelper.用它来进行解析就可以了. 6 触发开始语音识别 拖动一个Button,给一个响应事件,用于开始监听语音识别
//启动识别服务

[_iflyRecognizerView start];

5.2、无界面提示的语音识别
无界面提示的语音识别适合将语音识别放在后台,这个看具体的使用场景.无界面的方式相对来说简洁大方,可制定性高.
1 导入头文件
将讯飞SDK中的所有类都导入进来
#import < iflyMSC/iflyMSC.h >
2 登陆讯飞服务器在使用讯飞的语音解析之前,需要进行用户身份验证,即登陆讯飞服务器,这个在viewDidLoad()方法中添加两行代码即可.后面的ID数字就是之前我们在开放平台创建按自己应用时给的APPID,在下载的SDK中也是有的.

NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"565e4dd9"];
[IFlySpeechUtility createUtility:initString];

3 创建无界面提示的语音识别对象创建一个讯飞语音识别对象,可以对他进行一系列的调用

@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;//不带界面的识别对象

4 对象初始化前面声明了一些在语音合成的时候会用到的对象,现在需要对前面的对象进行初始化.还是在ViewDidLoad()里面进行就可以了.这里的初始化内容比较多,是进行一些语音的设置,所以单独做成了一个方法,在ViewDidLoad()中进行调用即可.

-(void)initRecognizer{
 //单例模式,无UI的实例
 if (_iFlySpeechRecognizer == nil) {
    _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];            
    [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];

  //设置听写模式
  [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
  }
_iFlySpeechRecognizer.delegate = self;    
if (_iFlySpeechRecognizer != nil) {
   IATConfig *instance = [IATConfig sharedInstance];     
    //设置最长录音时间
   [_iFlySpeechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
   //设置后端点
    [_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
//设置前端点
[_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
//网络等待时间
[_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];    
//设置采样率,推荐使用16K
[_iFlySpeechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];    
if ([instance.language isEqualToString:[IATConfig chinese]]) {
    //设置语言
    [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
    //设置方言
    [_iFlySpeechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
}else if ([instance.language isEqualToString:[IATConfig english]]) {
    [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
}
//设置是否返回标点符号
[_iFlySpeechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];    
 }
}

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];

6.语音合成
语音合成和语音识别的过程差不多
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 创建有界面提示的语音识别对象创建一个讯飞语音识别对象,可以对他进行一系列的调用

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()里面进行就可以了.

*instance = [TTSConfig sharedInstance];
   1	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的响应时间是将输入框中的文本内容读出来.

if ([self.VoiceText.text isEqualToString:@""]) {
return;
}
if (_audioPlayer != nil && _audioPlayer.isPlaying == YES) {
[_audioPlayer stop];
}
_synType = NomalType;
self.hasError = NO;
[NSThread sleepForTimeInterval:0.05];
_iFlySpeechSynthesizer.delegate = self;
[_iFlySpeechSynthesizer startSpeaking:self.VoiceText.text];