TTS語音合成技術(shù)-基礎(chǔ)

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();
    } 
  1. ActivityGeneralInterface接口
public interface ActivityGeneralInterface {

    void initViews();
    void initData();
    void initListeners();
}
  1. 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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,150評(píng)論 25 708
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,041評(píng)論 2 59
  • 面試必背 會(huì)舍棄、總結(jié)概括——根據(jù)我這些年面試和看面試題搜集過來的知識(shí)點(diǎn)匯總而來 建議根據(jù)我的寫的面試應(yīng)對(duì)思路中的...
    luoyangzk閱讀 7,182評(píng)論 6 173
  • 作為一個(gè)“整理癖”重度患者,今天一回家莫名就開始瘋狂收拾。 把音樂放到最大,從書柜收拾出廢紙、...
    鍋水水閱讀 1,358評(píng)論 2 0
  • 曾經(jīng),我爬上山頂歌唱, 我的歌聲在林立的山中回蕩, 雄渾而嘹亮! 鳥兒也和我合唱, 歌聲清脆而悠揚(yáng)! 小溪?dú)g快地流...
    即刻飛翔閱讀 248評(píng)論 2 5

友情鏈接更多精彩內(nèi)容