首页 > 代码库 > Android语音合成(TTS)SDK使用方法
Android语音合成(TTS)SDK使用方法
语音合成是实现人机语音交互,建立一个有听和讲能力的交互系统所必需的关键技术。
这篇文章将介绍如何使用百度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");
- }
如果需要对音频播报的音频流进行设置,可以调用如下接口
[java] view plaincopy
- speechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC);
[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) {
- }
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。