注册讯飞账号
略
实名认证
略
创建应用
![这里有一张图片]()
![这里有一张图片]()
添加语音合成服务
进入控制台,点击我的应用,然后添加语音合成服务(免费),并记下APPID
![这里有一张图片]()
下载SDK
点击应用右侧的SDK下载
![这里有一张图片]()
![这里有一张图片]()
你将会下载到一个压缩包:
![这里有一张图片]()
解压下载到的SDK压缩包
应用到Java项目
创建一个Java项目,然后把压缩包中的lib目录下的的所有东西全部复制粘贴到这个Java项目中:
![这里有一张图片]()
然后把jar包添加到build path中:
![这里有一张图片]()
到这里项目就搭建好了,下面来创建测试类。
创建测试类
1 设置合成监听器
在测试类中粘贴如下代码:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | static SynthesizeToUriListener synthesizeToUriListener = new SynthesizeToUriListener()
 {
 
 public void onBufferProgress(int progress)
 {
 }
 
 
 public void onSynthesizeCompleted(String uri, SpeechError error)
 {
 }
 @Override
 public void onEvent(int arg0, int arg1, int arg2, int arg3, Object arg4,
 Object arg5)
 {
 
 }
 };
 
 | 
设置好监听器之后,下面就可来合成并下载音频了。
编写main方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | public static void main(String[] args){
 
 SpeechUtility.createUtility(SpeechConstant.APPID + "=XXXXXXXX");
 
 SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer();
 
 
 mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyu");
 
 mTts.setParameter(SpeechConstant.SPEED, "50");
 
 mTts.setParameter(SpeechConstant.PITCH, "50");
 
 mTts.setParameter(SpeechConstant.VOLUME, "50");
 
 String text="测试语音合成";
 
 mTts.synthesizeToUri(text, "./tts_test.pcm",
 synthesizeToUriListener);
 }
 
 | 
完整下载Java demo如下
| 12
 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
 
 | package demo;import com.iflytek.cloud.speech.SpeechConstant;
 import com.iflytek.cloud.speech.SpeechError;
 import com.iflytek.cloud.speech.SpeechSynthesizer;
 import com.iflytek.cloud.speech.SpeechUtility;
 import com.iflytek.cloud.speech.SynthesizeToUriListener;
 public class DownloadDemo
 {
 public static void main(String[] args)
 {
 
 SpeechUtility.createUtility(SpeechConstant.APPID + "=5c80ae6b");
 
 SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer();
 
 
 mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyu");
 
 mTts.setParameter(SpeechConstant.SPEED, "50");
 
 mTts.setParameter(SpeechConstant.PITCH, "50");
 
 mTts.setParameter(SpeechConstant.VOLUME, "50");
 
 String text = "测试语音合成";
 
 mTts.synthesizeToUri(text, "./tts_test.pcm", synthesizeToUriListener);
 }
 
 static SynthesizeToUriListener synthesizeToUriListener = new SynthesizeToUriListener()
 {
 
 public void onBufferProgress(int progress)
 {
 }
 
 
 public void onSynthesizeCompleted(String uri, SpeechError error)
 {
 }
 @Override
 public void onEvent(int arg0, int arg1, int arg2, int arg3, Object arg4,
 Object arg5)
 {
 
 }
 };
 }
 
 
 | 
运行上面程序即可合成测试语音合成这个音频信息,保存在./tts_test.pcm中,不过这个文件好像不可直接打开播放,我用audition打开
用audition cs6 打开
采样率选择16000,其他默认,然后就可以播放了,如果播放的声音跟猪叫差不多的话,那应该是采样率错了,反正采样率多测试几次下就好了
![这里有一张图片]()
语音合成的地方肯定有些合成的不如意的地方,可以在audition中再次编辑,然后保存为mp3等等即可,这样以后就可直接播放了。