标签:tts 语音合成 android tts android语音合成
依托于百度开放云,百度语音为合作伙伴提供了业界领先、永久免费的语音技术服务,目前已上线的服务包括语音识别、语义解析、语音合成,后续会继续带来开放资源、多轮对话等技术服务;通过SDK、REST API及离线开发包等多种服务方式,满足不同开发者的开发需求。
目前百度语音提供免费的语音接入,语音合成质量尚可,但还没开放离线TTS。
具体详情可查看:http://yuyin.baidu.com/dev.php
导入百度语音的相关jar包和so文件如图:
提示:
将开发包中的 libs目录整体拷贝到工程目录,libs目录包括了各平台的 SO库,开发者视应用需要可以进行删减。galaxy_lite.jar是百度 Android公共基础库,如果项目中还集成了其它百度 SDK,如 PushSDK,在打包过程中出现类似如下的错误信息:
[2013-10-22 11:02:57 - Dex Loader] Unableto execute dex: Multiple dex files defineLcom/baidu/android/common/logging/Configuration; [2013-10-22 11:02:57 -VoiceRecognitionDemo] Conversion to Dalvik format failed: Unable to executedex: Multiple dex files define Lcom/baidu/android/common/logging/Configuration;
请将此 Jar 包移除。如果 Eclipse ADT版本插件低于 17,需要手工添加依赖库,添加方法为:Project => Properties => Java Build Path => Libraries => AddJAR... 3
为了方便使用我这里将百度语音合成引擎做了进一步的封装,封装成了SpeechUtil以方便调用:
package com.jph.tts; import android.content.Context; import android.media.AudioManager; import android.util.Log; import com.baidu.speechsynthesizer.SpeechSynthesizer; import com.baidu.speechsynthesizer.SpeechSynthesizerListener; import com.baidu.speechsynthesizer.publicutility.SpeechError; /** * 语音合成工具类 * * @author JPH * @date 2015-4-14 下午1:33:37 */ public class SpeechUtil implements SpeechSynthesizerListener { protected static final int UI_LOG_TO_VIEW = 0; private SpeechSynthesizer speechSynthesizer; private Context context; public SpeechUtil(Context activity) { this.context = activity; init(); } /** * 初始化合成相关组件 * * @author JPH * @date 2015-4-14 下午1:36:53 */ private void init() { speechSynthesizer = new SpeechSynthesizer(context, "holder", this); // 此处需要将setApiKey方法的两个参数替换为你在百度开发者中心注册应用所得到的apiKey和secretKey speechSynthesizer.setApiKey("your apiKey", "your secretKey"); speechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC); // activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); setParams(); } /** * 开始文本合成并朗读 * @author JPH * @date 2015-4-14 下午1:47:05 * @param content */ public void speak(final String content) { new Thread(new Runnable() { @Override public void run() { // setParams(); int ret = speechSynthesizer.speak(content.toString()); if (ret != 0) { Log.e("inf","开始合成器失败:"+ret); } } }).start(); } /** * 取消本次合成并停止朗读 * @author JPH * @date 2015-4-14 下午2:20:33 */ public void cancle() { speechSynthesizer.cancel(); } /** * 暂停文本朗读,如果没有调用speak(String)方法或者合成器初始化失败,该方法将无任何效果 * @author JPH * @date 2015-4-14 下午2:21:07 */ public void pause() { speechSynthesizer.pause(); } /** * 继续文本朗读,如果没有调用speak(String)方法或者合成器初始化失败,该方法将无任何效果 * @author JPH * @date 2015-4-14 下午2:21:29 */ public void resume() { speechSynthesizer.resume(); } /** * 为语音合成器设置相关参数 * @author JPH * @date 2015-4-14 下午1:45:11 */ private void setParams() { speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");//发音人,目前支持女声(0)和男声(1) speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "9");//音量,取值范围[0, 9],数值越大,音量越大 speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");//朗读语速,取值范围[0, 9],数值越大,语速越快 speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");//音调,取值范围[0, 9],数值越大,音量越高 speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, SpeechSynthesizer.AUDIO_ENCODE_AMR);//音频格式,支持bv/amr/opus/mp3,取值详见随后常量声明 speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, SpeechSynthesizer.AUDIO_BITRATE_AMR_15K85);//音频比特率,各音频格式支持的比特率详见随后常量声明 } @Override public void onStartWorking(SpeechSynthesizer synthesizer) { Log.i("msg", "开始工作,请等待数据..."); } @Override public void onSpeechStart(SpeechSynthesizer synthesizer) { Log.i("msg", "朗读开始"); } @Override public void onSpeechResume(SpeechSynthesizer synthesizer) { Log.i("msg", "朗读继续"); } @Override public void onSpeechProgressChanged(SpeechSynthesizer synthesizer, int progress) { // TODO Auto-generated method stub } @Override public void onSpeechPause(SpeechSynthesizer synthesizer) { Log.i("msg", "朗读已暂停"); } @Override public void onSpeechFinish(SpeechSynthesizer synthesizer) { Log.i("msg", "朗读已停止"); } @Override public void onNewDataArrive(SpeechSynthesizer synthesizer, byte[] audioData, boolean isLastData) { Log.i("msg", "新的音频数据:" + audioData.length + (isLastData ? "(end)" : "")); } @Override public void onError(SpeechSynthesizer synthesizer, SpeechError error) { Log.i("msg", "发生错误:" + error.errorDescription + "(" + error.errorCode + ")"); } @Override public void onCancel(SpeechSynthesizer synthesizer) { Log.i("msg", "已取消"); } @Override public void onBufferProgressChanged(SpeechSynthesizer synthesizer, int progress) { // TODO Auto-generated method stub } }
使用方法:
package com.jph.tts; import android.app.Activity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.baidu.tts.sample.R; public class HomeActivity extends Activity implements OnClickListener { protected static final int UI_LOG_TO_VIEW = 0; private TextView logView; private EditText inputTextView; private Button startButton; private SpeechUtil speechUtil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); logView = (TextView) findViewById(R.id.logView); logView.setMovementMethod(new ScrollingMovementMethod()); inputTextView = (EditText) findViewById(R.id.inputTextView); speechUtil = new SpeechUtil(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: speechUtil.speak(inputTextView.getText().toString()); break; case R.id.btnPause: speechUtil.pause(); break; case R.id.btnResume: speechUtil.resume(); break; case R.id.btnStop: speechUtil.cancle(); break; default: break; } } }
因为百度TTS需要联网解析所以要赋予应用相应的权限:
<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"/>推荐阅读:
【Android语音合成TTS】国内主流引擎对比
【Android语音合成TTS】云知声离线TTS使用详解
【Android语音合成TTS】百度语音接入方法,和使用技巧详解
标签:tts 语音合成 android tts android语音合成
原文地址:http://blog.csdn.net/fengyuzhengfan/article/details/45053177