如何实现语音合成呢?
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
2
|
#import <UIKit/UIKit.h>
#import "iflyMSC/IFlySpeechSynthesizerDelegate.h"
|
MainViewController.m
1
2
3
4
5
6
7
|
#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import "iflyMSC/IFlySpeechConstant.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "iflyMSC/IFlySpeechSynthesizer.h"
|
3.完成这些准备工作之后,接下来就是堆代码的工作了。为了方便,笔者只用了两个控件:一个UITextField、一个UIButton,然后给这两个控件分别做一个Outlet和Action连接。
MainViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#import <UIKit/UIKit.h>
#import "iflyMSC/IFlySpeechSynthesizerDelegate.h"
//引入语音合成类
@
class
IFlySpeechSynthesizer;
@
class
IFlyDataUploader;
//注意要添加语音合成代理
@interface MainViewController : UIViewController<IFlySpeechSynthesizerDelegate>
//声明语音合成的对象
@property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;
@property (strong, nonatomic) IBOutlet UITextField *content;
- (IBAction)Start:(id)sender;
@end
|
MainViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import "iflyMSC/IFlySpeechConstant.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "iflyMSC/IFlySpeechSynthesizer.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (
void
)viewDidLoad
{
[super viewDidLoad];
//通过appid连接讯飞语音服务器,把@"53b5560a"换成你申请的appid
NSString *initString = [[NSString alloc] initWithFormat:@
"appid=%@,timeout=%@"
,@
"53b5560a"
,@
"20000"
];
//所有服务启动前,需要确保执行createUtility
[IFlySpeechUtility createUtility:initString];
//创建合成对象,为单例模式
_iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
_iFlySpeechSynthesizer.delegate = self;
//设置语音合成的参数
//合成的语速,取值范围 0~100
[_iFlySpeechSynthesizer setParameter:@
"50"
forKey:[IFlySpeechConstant SPEED]];
//合成的音量;取值范围 0~100
[_iFlySpeechSynthesizer setParameter:@
"50"
forKey:[IFlySpeechConstant VOLUME]];
//发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表
[_iFlySpeechSynthesizer setParameter:@
"xiaoyan"
forKey:[IFlySpeechConstant VOICE_NAME]];
//<span id="0_nwp"><a id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4422%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=%D2%F4%C6%B5&k0=ios&kdi0=8&k1=%C8%ED%BC%FE%BF%AA%B7%A2&kdi1=1&k2=%D2%F4%C6%B5&kdi2=1&k3=%C8%ED%BC%FE&kdi3=1&k4=%D1%A7%CF%B0&kdi4=1&k5=mark&kdi5=1&sid=97ba2af8724f24b&ch=0&tu=u1704338&jk=05793bdbb5814e24&cf=29&fv=15&stid=9&urlid=0&luki=3&seller_id=1&di=128" target="_blank" mpid="0" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px">音频</span></a></span>采样率,目前支持的采样率有 16000 和 8000
[_iFlySpeechSynthesizer setParameter:@
"8000"
forKey:[IFlySpeechConstant SAMPLE_RATE]];
asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是documents
[_iFlySpeechSynthesizer setParameter:
"tts.pcm"
forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
//隐藏键盘,点击空白处
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tapGr.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGr];
}
-(
void
)viewTapped:(UITapGestureRecognizer*)tapGr
{
[self.content resignFirstResponder];
}
- (
void
)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Start:(id)sender
{
//启动合成会话
[_iFlySpeechSynthesizer startSpeaking:self.content.text];
}
#pragma <span id="1_nwp"><a id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F4422%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=mark&k0=ios&kdi0=8&k1=%C8%ED%BC%FE%BF%AA%B7%A2&kdi1=1&k2=%D2%F4%C6%B5&kdi2=1&k3=%C8%ED%BC%FE&kdi3=1&k4=%D1%A7%CF%B0&kdi4=1&k5=mark&kdi5=1&sid=97ba2af8724f24b&ch=0&tu=u1704338&jk=05793bdbb5814e24&cf=29&fv=15&stid=9&urlid=0&luki=6&seller_id=1&di=128" target="_blank" mpid="1" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px">mark</span></a></span> - IFlySpeechSynthesizerDelegate
//开始播放
- (
void
) onSpeakBegin
{
}
//缓冲进度
- (
void
) onBufferProgress:(
int
) progress message:(NSString *)msg
{
NSLog(@
"bufferProgress:%d,message:%@"
,progress,msg);
}
//播放进度
- (
void
) onSpeakProgress:(
int
) progress
{
NSLog(@
"play progress:%d"
,progress);
}
//暂停播放
- (
void
) onSpeakPaused
{
}
//恢复播放
- (
void
) onSpeakResumed
{
}
//结束回调
- (
void
) onCompleted:(IFlySpeechError *) error
{
}
@end
|
4.以上的代理方法其实是可以不写的,但是官方给出的说明是需要加上的。若是在运行过程中出现错误,可以查看开发者文档的错误码列表,找出相应的错误。如果大家对以上的讲解还有不懂的地方,请仔细阅读官方提供的sdk文件,里面有详细的说明,也可以给我留言。