Android语音合成(TTS)SDK使用方法
- <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对象
- speechSynthesizer = new SpeechSynthesizer(getApplicationContext(),
- "holder", this);
- // 此处需要将setApiKey方法的两个参数替换为你在百度开发者中心注册应用所得到的apiKey和secretKey
- speechSynthesizer.setApiKey("your-apiKey", "your-secretKey");
- 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");
- }
- speechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- 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) {
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。