TTS语音合成技术-进阶
2018-10-11 本文已影响15人
瑟闻风倾
1. TextToSpeechUtil2类
package comi.example.liy.firstbasicproject.tool;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;
import java.util.Locale;
import comi.example.liy.firstbasicproject.R;
/**
* Created by liy on 2018-07-11.
*/
public class TextToSpeechUtil2 {
private static TextToSpeech textToSpeech;
//获取TextToSpeech对象并设置相关属性
public static void speak(final Context context, final String text){
if(textToSpeech == null){
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {//装载TTS引擎成功
//Locale.getDefault()获取系统默认的区域信息:如系统语言设置为中文则参数为 "zho","CHN",设置为美式英语则参数为 "eng","USA"
int result = textToSpeech.setLanguage(new Locale(Locale.getDefault().getISO3Language(),Locale.getDefault().getISO3Country()));
Log.v("ttSpeech_result",result+","+ Locale.getDefault().getISO3Language() + ","+ Locale.getDefault().getISO3Country() + "");//如果打印为-2,说明不支持这种语言
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(context,context.getString(R.string.missing_or_unsupported), Toast.LENGTH_SHORT).show();
}else {
textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);// 设置语速
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH, null);
}
}else {
Toast.makeText(context,context.getString(R.string.load_tts_fail), Toast.LENGTH_SHORT).show();
}
}
});
}else {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH, null);
}
}
//释放TextToSpeech对象
public static void shutdown(){
if (textToSpeech != null) {
textToSpeech.shutdown();
}
}
}
2. HomeActivity
public class HomeActivity extends Activity implements ActivityGeneralInterface {
private Button btnTextToSpeech2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initViews();
initData();
initListeners();
}
@Override
public void initViews() {
btnTextToSpeech2 = (Button)findViewById(R.id.activity_main_textToSpeech2);
}
@Override
public void initData() {
}
@Override
public void initListeners() {
}
public void TextToSpeechOnClick2(View view){
TextToSpeechUtil2.speak(this,getString(R.string.text_to_speech));
}
@Override
protected void onDestroy() {
super.onDestroy();
textToSpeechUtil.shutdown();
}
- ActivityGeneralInterface接口
public interface ActivityGeneralInterface {
void initViews();
void initData();
void initListeners();
}
- activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/activity_main_textToSpeech2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextToSpeech2"
android:textAllCaps="false"
android:onClick="TextToSpeech2OnClick"/>
</LinearLayout>
5、资源文件string.xml
<string name="text_to_speech">文字转语音</string>