Android原⽣TTS的基本使⽤以及配合中⽂语⾳包实现中⽂TTS
最近因为⼀些特殊需求,公司希望使⽤离线TTS,不花钱的中⽂离线TTS,最好⾳⾊还要多⼀点,语速,语调都要可控;总之⼀句话,以不花钱的代价达到最好的效果。其实现在很多的开发者平台也都提供⼀两种⾳⾊的离线TTS开发SDK,⽐如科⼤,云知声等等,我之前也做过⼀个百度在线ASR与云知声离线TTS结合实现的⼀个复读机的⼩Demo,有兴趣的朋友
配合最近我上传的以及本篇()的介绍,就可以实现TTS播报,暂停以及恢复等功能
因为了解过Android有原⽣的TTS功能,于是就简单使⽤了⼀下,Google的产品不⽀持中⽂也很容易理解,毕竟市场都不给别⼈,⽽且中⽂的处理相较于英⽂等处理起来也⽐较⿇烦,所以,原⽣的TTS是不⽀持中⽂的。但是⾥⾯是有预留的常量中⽂普通话,台湾话等,但是是设置不了的。所以在具体使⽤的时候需要借助中⽂TTS引擎的帮助,所以就需要安装其他的软件或者服务,⽹上推荐⽐较多的是科⼤讯飞+这个APK,⾥⾯可以设置发⾳⼈,语速,语调等。我简单看了⼀下,⾥⾯⽀持五种普通话⾳⾊;但是我们从科⼤官⽹上看上⾯只提供两种离线的⾳⾊语⾳;所以使⽤这个APK可以丰富⼀下你的离线TTS⾳⾊;从另⼀个⽅⾯来说,不知道有没有考虑过,如果我们能到⼀个开源框架可以实现加载这些离线语⾳包,然后想办法获取到这些离线语⾳包,那么我们就可以多使⽤其另外的三种离线语⾳包。但是这样的开源框架估计不好。其他还有度秘语⾳引擎,google的⽂字转语⾳引擎;,感谢这位博主。说的好像有点多了,下⾯进⼊正题;今天主要介绍⼀下Android原⽣的TTS接⼝的基本使⽤。
1,主要使⽤步骤
(1)新建⼀个类(内部类也是可以的),实现OnInitListener接⼝,重写onInit()⽅法,通常是判断TTS引擎初始化的状态
private class TTSListener implements OnInitListener {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
// int supported = mSpeech.setLanguage(Locale.US);
// if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
// Toast.makeText(MainActivity.this, "不⽀持当前语⾔!", Toast.LENGTH_SHORT).show();
// Log.i(TAG, "onInit: ⽀持当前选择语⾔");
// }else{
//
// }
Log.i(TAG, "onInit: TTS引擎初始化成功");
}
else{
Log.i(TAG, "onInit: TTS引擎初始化失败");
}
}
}
(2)获取TTS引擎
mSpeech = new TextToSpeech(MainActivity.this, new TTSListener());
(3)在使⽤的时候,如果有需要可以调整TTS引擎参数,包括上⾯说的语速,语调,语⾔等等(当然,当前不⽀持中⽂,使⽤的话,先下载上⾯提到的服务或者应⽤并安装,然后在“设置”--》“语⾳与输⼊”--》“⽂本转语⾳输出”--》选择你安装中⽂TTS就可以了)
mSpeech.setLanguage((choosedLanguage));
mSpeech.setSpeechRate(SharedData.voice_speed);
mSpeech.setPitch(SharedData.voice_pitch);
2,⽰例代码:
⽰例代码很简单,我简单加了⼀点东西,也很好理解。
MainActivity.java代码:
package com.hfut.operationandroidtts;
import android.app.AlertDialog;
import s.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import s.TextToSpeech;
import s.TextToSpeech.OnInitListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TextToSpeech mSpeech = null;
private EditText edit = null;
private String choosedLanguage;
private RadioButton english,chainese,german,french,taiWan;
private RadioGroup languageGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
initUI();
initLanguageList();
mSpeech = new TextToSpeech(MainActivity.this, new TTSListener());
}
private void initLanguageList() {
SharedData.languageList.put("英语",Locale.ENGLISH);
SharedData.languageList.put("中⽂",Locale.CHINESE);
SharedData.languageList.put("德语",Locale.GERMAN);
SharedData.languageList.put("法语",Locale.FRENCH);
SharedData.languageList.put("台湾话",Locale.TAIWAN);
}
public void openAudioFile(View view) {
choosedLanguage=getLanguage(languageGroup);
int supported = mSpeech.setLanguage((choosedLanguage));
mSpeech.setSpeechRate(SharedData.voice_speed);
mSpeech.setPitch(SharedData.voice_pitch);
Log.i(TAG, "选择语⾔: "+choosedLanguage+"--"+(choosedLanguage));
/
/mSpeech.setAudioAttributes(new AudioAttributes());
// mSpeech.setVoice(new Voice(null,Locale.US,Voice.QUALITY_HIGH,Voice.LATENCY_NORMAL,false,null));
// mSpeech.setVoice(new Voice(null,Locale.US,Voice.QUALITY_HIGH,Voice.LATENCY_NORMAL,false,null));
if((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)){ //语⾔设置失败
Log.i(TAG, "语⾔设置失败: "+choosedLanguage);
}
else{
Log.i(TAG, "语⾔设置成功: "+choosedLanguage);
}
String tempStr = Text().toString();
mSpeech.speak(tempStr, TextToSpeech.QUEUE_FLUSH, null);
Log.i(TAG, "测试⽂本: "+tempStr);
Log.i(TAG, "当前语速: "+SharedData.voice_speed+",最快语速1.5");
Log.i(TAG, "当前⾳调:"+SharedData.voice_pitch+",最⾼⾳调2.0");
//Log.i(TAG, "test: 执⾏了");
}
//保存⾳频⽂件
public void saveAudioFile(View view) {
HashMap<String, String> myHashRender = new HashMap<>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Text().toString());
mSpeech.Text().toString(), myHashRender,
"/mnt/sdcard/"+ new Date().toString().replace(" ","_").trim()+".wav");
Log.i(TAG, "saveAudioFile: "+"/mnt/sdcard/"+ new Date().toString().replace(" ","_").trim()+".wav"+"⽂件保存成功");
Toast.makeText(this,"⽂件保存成功",Toast.LENGTH_SHORT).show();
}
private String getLanguage(RadioGroup languageGroup) {
int CheckedRadioButtonId();
String tempStr="";
if(choosedButtonID==Id()){
tempStr="英语";
}
else if(choosedButtonID==Id()){
tempStr="中⽂";
}
else if(choosedButtonID==Id()){
tempStr="德语";
}
else if(choosedButtonID==Id()){
tempStr="法语";
}
else if(choosedButtonID==Id()){
tempStr="台湾话";
}
else{
}
return tempStr;
}
//增加⾳量
public void increVoice(View view){
if(SharedData.voice_speed>=1.5f){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("速度已经最快,⽆法调整");
dialog.show();
}
else{
SharedData.voice_speed+=0.1f;
}
alertdialog使用方法}
//减⼩⾳量
public void decreVoice(View view){
if(SharedData.voice_speed<=0.1f){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("速度已经最慢,⽆法调整");
dialog.show();
}
else{
SharedData.voice_speed-=0.1f;
}
}
//升⾼⾳调
public void increPitch(View view){
if(SharedData.voice_pitch>=2.0f){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("⾳调已经最⾼,⽆法调整");
dialog.show();
}
else{
SharedData.voice_pitch+=0.1f;
}
}
//减低⾳调
public void decrePitch(View view){
if(SharedData.voice_pitch<=0.1f){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("⾳调已经最低,⽆法调整");
dialog.show();
}
else{
SharedData.voice_pitch-=0.1f;
}
}
private class TTSListener implements OnInitListener {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
/
/ int supported = mSpeech.setLanguage(Locale.US);
// if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) { // Toast.makeText(MainActivity.this, "不⽀持当前语⾔!", Toast.LENGTH_SHORT).show();
// Log.i(TAG, "onInit: ⽀持当前选择语⾔");
// }else{
//
// }
Log.i(TAG, "onInit: TTS引擎初始化成功");
}
else{
Log.i(TAG, "onInit: TTS引擎初始化失败");
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (mSpeech != null) {
mSpeech.stop();
mSpeech.shutdown();
mSpeech = null;
}
}
}
SharedData.java代码:
package com.hfut.operationandroidtts;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* author:why
* created on: 2018/6/5 11:28
* description:
*/
public class SharedData {
//语速
public static float voice_speed=0.5f;
//⾳调
public static float voice_pitch=1.0f;
//
public static Map<String,Locale> languageList=new HashMap<String,Locale>(); }
l代码:
<LinearLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/test_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="openAudioFile"
android:text="播放合成语⾳"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:onClick="saveAudioFile"
android:text="保存合成⾳频"
android:textSize="20dp" />
<LinearLayout
android:layout_marginLeft="160dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论