android原生TTS+语音引擎 实现纯离线 免费的中英TTS
之前使用百度的语音合成 sdk做了个简单的tts,但是它不是真正的免费和纯离线的,所以在查阅相关资料后,使用Android的原生TTS ,因为它不支持中文,需要借助其他语音引擎可实现纯离线 免费的TTS。
Android原生的TTS是不支持中文合成的,需要借助其他的语音引擎(apk安装包),比如科大讯飞语音引擎3.0,度秘语音引擎3.0以及新版手机基本都内置有语音引擎,可在设置——》语言——》首选引擎进行选择。
具体可参考这篇csdn博客:https://blog.csdn.net/yingchengyou/article/details/79591954
我手机是荣耀10,内置有讯飞语音引擎,其他两个可以在上面网址下载到手机安装。
点击科大讯飞 !可选择发音人,内置语言引擎和讯飞的中英文只有女声,而度秘的在我手机选择不了,估计是版本问题吧。
Android的TextToSpeech类文档可参考这篇csdn博客:https://blog.csdn.net/qq_26971803/article/details/51176592
Android原生TTS类——TextToSpeech使用方法非常简单。
1.实例化 private TextToSpeech textToSpeech = new TextToSpeech(this, this);
2.重写onInit方法,进行设置语言进行初始化 int result = textToSpeech.setLanguage(Locale.CHINA);
3.设置tts参数textToSpeech.setPitch(1.4f);// 设置音调,1.0是常规
textToSpeech.setSpeechRate(1.2f);//设定语速,1.0正常语速
4.合成并播放 textToSpeech.speak(et_input.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
5.释放资源 textToSpeech.stop(); textToSpeech.shutdown();
该demo缺陷:未实现暂停,恢复播放功能。TextToSpeech未提供暂停,恢复播放方法,如果想实现的话,可以用synthesizeToFile方法保存音频文件,再用MediaPlayer对音频文件进行操作。
具体代码如下:
import android.speech.tts.TextToSpeech;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Locale;public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener { private EditText et_input; private Button bt_start; private Button bt_pause; private Button bt_resume; private TextToSpeech textToSpeech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (textToSpeech==null){ textToSpeech = new TextToSpeech(this, this); } initView(); } private void ttsParam() { textToSpeech.setPitch(1.4f);// 设置音调,,1.0是常规 textToSpeech.setSpeechRate(1.2f);//设定语速,1.0正常语速 } private void initView() { et_input = findViewById(R.id.et_input); bt_start = findViewById(R.id.bt_start); bt_pause = findViewById(R.id.bt_pause); bt_resume = findViewById(R.id.bt_resume); bt_start.setOnClickListener(this); bt_pause.setOnClickListener(this); bt_resume.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_start: textToSpeech.speak(et_input.getText().toString(), TextToSpeech.QUEUE_FLUSH, null); break; case R.id.bt_pause: Toast.makeText(this, "未实现", Toast.LENGTH_SHORT).show(); break; case R.id.bt_resume: Toast.makeText(this, "未实现", Toast.LENGTH_SHORT).show(); break; } } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { //初始化tts引擎 int result = textToSpeech.setLanguage(Locale.CHINA); //设置参数 ttsParam(); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText(this, "语音包丢失或语音不支持", Toast.LENGTH_SHORT).show(); } } } @Override protected void onDestroy() { if (textToSpeech!=null){ //释放资源 textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); }}
xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
github:https://github.com/sunfusong/NativeTTS
更多相关文章
- 【Android语音合成TTS】云知声离线TTS使用详解
- [置顶] 3D引擎--可移植到Android的开源的引擎
- 人工智能交互集成在线语音合成能力的Tips
- android 中文语音
- Chrome V8 引擎移植到 Android
- 从android角度看2D加速图形引擎
- 基于WiEngine游戏引擎--背景移动
- Android应用开发揭秘(半年重印4次,内容已更新至Android(安卓)2.2
- Android(安卓)游戏引擎libgdx 如何添加万普类广告条