最近项目需要,接触了科大讯飞的语音合成系统,可以实现语音合成,语音播放等等功能,非常的实用
1.去科大讯飞 注册用户
http://passport.xfyun.cn/register
2.注册ok ,用找好密码登录,并进入控制台
注册成功后,每个应用会有一个id ,这个在开发的时候会用到
3.添加语音合成服务
4.点击上图右上角的部分 下载sdk
5.下载完后 解压,可以看到,里面专门有给 树莓派的库,有空可以试一试
6.今天主要试一试 语音播放,输入一个文字,播放出声音
打开 eclipse ,新建一个java project,把SDK里面的各种包全部导入eclipse
新建一个类
直接上代码
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.SynthesizerListener;
public class SpeakTest {
public static void main(String[] args) {
SpeechUtility.createUtility( SpeechConstant.APPID +"=这个地方填写申请的ID");
SpeechSynthesizer mTts= SpeechSynthesizer.createSynthesizer( );
mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//设置发音人
mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围 0~100 //设置合成音频保存位置(可自定义保存位置),保存在“./tts_test.pcm” //如果不需要保存合成音频,注释该行代码
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./tts_test.pcm"); //3.开始合成
mTts.startSpeaking("语音合成测试程序", mSynListener);
}
static SynthesizerListener mSynListener = new SynthesizerListener(){
//会话结束回调接口,没有错误时,error为null
public void onCompleted(SpeechError error) {} //缓冲进度回调 //percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲音频在 文本中结束位置,info为附加信息。
public void onBufferProgress(int percent, int beginPos, int endPos, String info) {} //开始播放
public void onSpeakBegin() {} //暂停播放
public void onSpeakPaused() {} //播放进度回调 //percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音频在 文本中结束位置.
public void onSpeakProgress(int percent, int beginPos, int endPos) {} //恢复播放回调接口
public void onSpeakResumed() {}
@Override
public void onEvent(int arg0, int arg1, int arg2, int arg3, Object arg4, Object arg5) {
} };
}
运行代码 ,电脑就会发出声音