语音合成是实现人机语音交互,建立一个有听和讲能力的交互系统所必需的关键技术。
这篇文章将介绍如何使用百度Android语音合成SDK。
与语音识别SDK类似,使用语音合成SDK也需要注册,并开启语音合成API服务,详细步骤可以参考 点击打开链接 中的注册部分。
百度语音合成SDK以JAR包+动态链接库形式发布,需要开发者在 点击打开链接 下载SDK开发包,并将libs文件夹拷贝到工程中
申请必要的权限
[java] view
plaincopy
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
初始化SpeechSynthesizer对象
[java] view
plaincopy
- speechSynthesizer = new SpeechSynthesizer(getApplicationContext(),
- "holder", this);
- // 此处需要将setApiKey方法的两个参数替换为你在百度开发者中心注册应用所得到的apiKey和secretKey
- speechSynthesizer.setApiKey("your-apiKey", "your-secretKey");
参数设置
[java] view
plaincopy
- private void setParams() {
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, "1");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, "4");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_LANGUAGE, "ZH");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_NUM_PRON, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_ENG_PRON, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PUNC, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_BACKGROUND, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_STYLE, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TERRITORY, "0");
- }
不需要实现这么多参数,具体参数的含义可以参看SDK的使用文档和参数设置。
如果需要对音频播报的音频流进行设置,可以调用如下接口
[java] view
plaincopy
- speechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC);
以上内容即可以实现语音合成的操作,识别过程中可以使用SpeechSynthesizerListener来监听状态,从而更好的实现界面同步。
[java] view
plaincopy
- class listener implements SpeechSynthesizerListener {
- @Override
- public void onStartWorking(SpeechSynthesizer synthesizer) {
- logDebug("开始工作,请等待数据...");
- }
- @Override
- public void onSpeechStart(SpeechSynthesizer synthesizer) {
- logDebug("朗读开始");
- }
- @Override
- public void onSpeechResume(SpeechSynthesizer synthesizer) {
- logDebug("朗读继续");
- }
- @Override
- public void onSpeechProgressChanged(SpeechSynthesizer synthesizer, int progress) {
- }
- @Override
- public void onSpeechPause(SpeechSynthesizer synthesizer) {
- logDebug("朗读已暂停");
- }
- @Override
- public void onSpeechFinish(SpeechSynthesizer synthesizer) {
- logDebug("朗读已停止");
- }
- @Override
- public void onNewDataArrive(SpeechSynthesizer synthesizer, byte[] dataBuffer, int dataLength) {
- logDebug("新的音频数据:" + dataLength);
- }
- @Override
- public void onError(SpeechSynthesizer synthesizer, SpeechError error) {
- logError("发生错误:" + error.errorDescription + "(" + error.errorCode + ")");
- }
- @Override
- public void onCancel(SpeechSynthesizer synthesizer) {
- logDebug("已取消");
- }
- @Override
- public void onBufferProgressChanged(SpeechSynthesizer synthesizer, int progress) {
- }
- }
百度语音合成(TTS)SDK使用方法
时间: 2024-11-10 07:56:26