1. TextToSpeechUtil类
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.
* 1、语音合成技术:TextToSpeech封装
* 2、软件设计模式之单例模式:一个类有且仅有一个实例,并且自行实例化向整个系统提供
* 2.1、实现:
* (1)单例模式的类只提供私有的构造函数:其他处的代码就无法通过调用该类的构造方法来实例化该类的对象,只有通过该类提供的静态方法来得到该类的唯一实例
* (2)类定义中含有一个该类的静态私有对象;
* (3)该类提供了一个静态的公有的函数用于创建或获取它本身的静态私有对象。
* 2.2、形式(8种):懒汉模式、饿汉模式和双重锁形式
* 2.3、注意事项:单例模式在多线程的应用场合下必须小心使用。
* 如果当唯一实例尚未创建时,有两个线程同时调用创建方法,那么它们同时没有检测到唯一实例的存在,从而同时各自创建了一个实例,这样就有两个实例被构造出来,从而违反了单例模式中实例唯一的原则。
* 解决这个问题的办法可采用懒汉模式及双重判断加同步的方式。
* 2.4、优点:系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能。
* 2.5、缺点:当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new,可能会给其他开发人员造成困扰,特别是看不到源码的时候。
* 2.6、适用场合:
* (1)需要频繁的进行创建和销毁的对象;
* (2)创建对象时耗时过多或耗费资源过多,但又经常用到的对象;
* (3)工具类对象;
* (4)频繁访问数据库或文件的对象。
*/
public class TextToSpeechUtil {
//如果面对高并发的情况,可采用懒汉模式及双重判断加同步的方式:线程安全;延迟加载;效率较高
private static TextToSpeechUtil instance = null;
//构造方法
private TextToSpeechUtil(Context context){
initTextToSpeech(context);
}
public static TextToSpeechUtil getInstance(Context context){
if(instance == null){
synchronized(TextToSpeechUtil.class){
if(instance==null){
instance = new TextToSpeechUtil(context);
}
}
}
return instance;
}
public static TextToSpeech textToSpeech;
//初始化TextToSpeech对象
private void initTextToSpeech(final Context context){
// 传入context及onInitListener
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(context.getString(R.string.hello), TextToSpeech.QUEUE_FLUSH, null);*/
}
}else {
Toast.makeText(context,context.getString(R.string.load_tts_fail), Toast.LENGTH_SHORT).show();
}
}
});
}
//释放TextToSpeech对象
public void shutdown(){
if (textToSpeech != null) {
textToSpeech.shutdown();
}
}
}
2. HomeActivity
public class HomeActivity extends Activity implements ActivityGeneralInterface {
private Button btnTextToSpeech;
private TextToSpeechUtil textToSpeechUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initViews();
initData();
initListeners();
}
@Override
public void initViews() {
btnTextToSpeech = (Button)findViewById(R.id.activity_main_textToSpeech);
}
@Override
public void initData() {
textToSpeechUtil = TextToSpeechUtil.getInstance(this);
}
@Override
public void initListeners() {
}
public void TextToSpeechOnClick(View view){
textToSpeechUtil.textToSpeech.speak(getString(R.string.text_to_speech), TextToSpeech.QUEUE_FLUSH, null);
}
@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_textToSpeech"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextToSpeech"
android:textAllCaps="false"
android:onClick="TextToSpeechOnClick"/>
</LinearLayout>
5、资源文件string.xml
<string name="text_to_speech">文字转语音</string>
备注:
「语音合成进阶」(https://www.jianshu.com/p/e2995383113b)
网友评论