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、語音合成技術(shù):TextToSpeech封裝
* 2、軟件設(shè)計(jì)模式之單例模式:一個(gè)類有且僅有一個(gè)實(shí)例,并且自行實(shí)例化向整個(gè)系統(tǒng)提供
* 2.1、實(shí)現(xiàn):
* (1)單例模式的類只提供私有的構(gòu)造函數(shù):其他處的代碼就無法通過調(diào)用該類的構(gòu)造方法來實(shí)例化該類的對(duì)象,只有通過該類提供的靜態(tài)方法來得到該類的唯一實(shí)例
* (2)類定義中含有一個(gè)該類的靜態(tài)私有對(duì)象;
* (3)該類提供了一個(gè)靜態(tài)的公有的函數(shù)用于創(chuàng)建或獲取它本身的靜態(tài)私有對(duì)象。
* 2.2、形式(8種):懶漢模式、餓漢模式和雙重鎖形式
* 2.3、注意事項(xiàng):?jiǎn)卫J皆诙嗑€程的應(yīng)用場(chǎng)合下必須小心使用。
* 如果當(dāng)唯一實(shí)例尚未創(chuàng)建時(shí),有兩個(gè)線程同時(shí)調(diào)用創(chuàng)建方法,那么它們同時(shí)沒有檢測(cè)到唯一實(shí)例的存在,從而同時(shí)各自創(chuàng)建了一個(gè)實(shí)例,這樣就有兩個(gè)實(shí)例被構(gòu)造出來,從而違反了單例模式中實(shí)例唯一的原則。
* 解決這個(gè)問題的辦法可采用懶漢模式及雙重判斷加同步的方式。
* 2.4、優(yōu)點(diǎn):系統(tǒng)內(nèi)存中該類只存在一個(gè)對(duì)象,節(jié)省了系統(tǒng)資源,對(duì)于一些需要頻繁創(chuàng)建銷毀的對(duì)象,使用單例模式可以提高系統(tǒng)性能。
* 2.5、缺點(diǎn):當(dāng)想實(shí)例化一個(gè)單例類的時(shí)候,必須要記住使用相應(yīng)的獲取對(duì)象的方法,而不是使用new,可能會(huì)給其他開發(fā)人員造成困擾,特別是看不到源碼的時(shí)候。
* 2.6、適用場(chǎng)合:
* (1)需要頻繁的進(jìn)行創(chuàng)建和銷毀的對(duì)象;
* (2)創(chuàng)建對(duì)象時(shí)耗時(shí)過多或耗費(fèi)資源過多,但又經(jīng)常用到的對(duì)象;
* (3)工具類對(duì)象;
* (4)頻繁訪問數(shù)據(jù)庫或文件的對(duì)象。
*/
public class TextToSpeechUtil {
//如果面對(duì)高并發(fā)的情況,可采用懶漢模式及雙重判斷加同步的方式:線程安全;延遲加載;效率較高
private static TextToSpeechUtil instance = null;
//構(gòu)造方法
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對(duì)象
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()獲取系統(tǒng)默認(rèn)的區(qū)域信息:如系統(tǒng)語言設(shè)置為中文則參數(shù)為 "zho","CHN",設(shè)置為美式英語則參數(shù)為 "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);// 設(shè)置音調(diào),值越大聲音越尖(女生),值越小則變成男聲,1.0是常規(guī)
textToSpeech.setSpeechRate(1.0f);// 設(shè)置語速
/*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對(duì)象
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">文字轉(zhuǎn)語音</string>
備注:
「語音合成進(jìn)階」(http://www.itdecent.cn/p/e2995383113b)