现在语音服务越来越热,我们平时使用的很多软件都带有语音合成和识别功能,用起来也很方便。说到语音服务,Google和微软都提供过API接口,不过笔者要介绍的是国内的智能语音技术提供商---科大讯飞。之前看过一个比较Google、微软和科大讯飞语音识别引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有兴趣可以去看看。笔者接触语音服务的时间也不长,对语音服务也不是很了解,但是拆解过科大讯飞的Demo,对语音服务的程序使用还是知道的。这次只整理了语音合成的代码,关于语音识别和其他的下次再发,废话完了进入正题。
如何实现语音合成呢?
1、注册讯飞账号,申请APPID(注意选择IOS平台)
2、加载所需要的类库
3、导入所需要的类库文件头
4、调用申请的APPID以及所需函数,完成语音合成(需要参考官方给出的SDK文件)
详细步骤:
一、首先到科大讯飞官网注册账号(http://open.voicecloud.cn/),并创建应用获取appid,下载sdk文件
二、代码实现api调用
1.先用xcode(我这里使用的是xcode 5.1)新建好一个项目,然后在项目添加要用的类库。其中有一个是讯飞语音的类库iflyMSC,在下载的sdk文件里有,导入就行了。导入的时候要注意把iflyMSC类库拷贝到你的工程目录里,不然后果很严重!
2.导完类库之后,在建好的工程里添加好要用的头文件。
MainViewController.h
1#import<UIKit/UIKit.h>2#import"iflyMSC/IFlySpeechSynthesizerDelegate.h"
MainViewController.m
1#import"MainViewController.h"2#import<QuartzCore/QuartzCore.h>3#import<AVFoundation/AVAudiosession.h>4#import<AudioToolbox/AudioSession.h>5#import"iflyMSC/IFlySpeechConstant.h"6#import"iflyMSC/IFlySpeechUtility.h"7#import"iflyMSC/IFlySpeechSynthesizer.h"
3.完成这些准备工作之后,接下来就是堆代码的工作了。为了方便,笔者只用了两个控件:一个UITextField、一个UIButton,然后给这两个控件分别做一个Outlet和Action连接。
MainViewController.h
1#import<UIKit/UIKit.h>2#import"iflyMSC/IFlySpeechSynthesizerDelegate.h"3//引入语音合成类4@classIFlySpeechSynthesizer;5@classIFlyDataUploader;6//注意要添加语音合成代理7@interfaceMainViewController : UIViewController<IFlySpeechSynthesizerDelegate>8//声明语音合成的对象9@PRoperty (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;10@property (strong, nonatomic) IBOutlet UITextField *content;1112- (IBAction)Start:(id)sender;13@end
MainViewController.m
1#import"MainViewController.h"2#import<QuartzCore/QuartzCore.h>3#import<AVFoundation/AVAudioSession.h>4#import<AudioToolbox/AudioSession.h>5#import"iflyMSC/IFlySpeechConstant.h"6#import"iflyMSC/IFlySpeechUtility.h"7#import"iflyMSC/IFlySpeechSynthesizer.h"89@interfaceMainViewController ()1011@end1213@implementationMainViewController1415- (void)viewDidLoad16{17[super viewDidLoad];18//通过appid连接讯飞语音服务器,把@"53b5560a"换成你申请的appid19NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"];20//所有服务启动前,需要确保执行createUtility21[IFlySpeechUtility createUtility:initString];2223//创建合成对象,为单例模式24_iFlySpeechSynthesizer =[IFlySpeechSynthesizer sharedInstance];25_iFlySpeechSynthesizer.delegate=self;2627//设置语音合成的参数28//合成的语速,取值范围 0~10029[_iFlySpeechSynthesizer setParameter:@"50"forKey:[IFlySpeechConstant SPEED]];30//合成的音量;取值范围 0~10031[_iFlySpeechSynthesizer setParameter:@"50"forKey:[IFlySpeechConstant VOLUME]];32//发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表33[_iFlySpeechSynthesizer setParameter:@"xiaoyan"forKey:[IFlySpeechConstant VOICE_NAME]];34//音频采样率,目前支持的采样率有 16000 和 800035[_iFlySpeechSynthesizer setParameter:@"8000"forKey:[IFlySpeechConstant SAMPLE_RATE]];36////asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是documents37[_iFlySpeechSynthesizer setParameter:"tts.pcm"forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];3839//隐藏键盘,点击空白处40UITapGestureRecognizer *tapGr =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];41tapGr.cancelsTouchesInView =NO;42[self.view addGestureRecognizer:tapGr];43}4445-(void)viewTapped:(UITapGestureRecognizer*)tapGr46{47[self.content resignFirstResponder];48}4950- (void)didReceiveMemoryWarning51{52[super didReceiveMemoryWarning];53//Dispose of any resources that can be recreated.54}5556- (IBAction)Start:(id)sender57{58//启动合成会话59[_iFlySpeechSynthesizer startSpeaking:self.content.text];60}6162#pragmamark - IFlySpeechSynthesizerDelegate63//开始播放64- (void) onSpeakBegin65{6667}6869//缓冲进度70- (void) onBufferProgress:(int) progress message:(NSString *)msg71{72NSLog(@"bufferProgress:%d,message:%@",progress,msg);73}7475//播放进度76- (void) onSpeakProgress:(int) progress77{78NSLog(@"play progress:%d",progress);79}8081//暂停播放82- (void) onSpeakPaused83{8485}8687//恢复播放88- (void) onSpeakResumed89{9091}9293//结束回调94- (void) onCompleted:(IFlySpeechError *) error95{9697}98@end
4.以上的代理方法其实是可以不写的,但是官方给出的说明是需要加上的。若是在运行过程中出现错误,可以查看开发者文档的错误码列表,找出相应的错误。如果大家对以上的讲解还有不懂的地方,请仔细阅读官方提供的sdk文件,里面有详细的说明,也可以给我留言。
PS:若本文有什么错误的地方,还望大家指正留言,我会尽快修改!