Android中使用TextToSpeech的方法
前言
在一年前,和朋友一起碼了一個(gè)英語APP,仿照某APP實(shí)現(xiàn)了單詞的功能,最開始就是借助的TextToSpeech,后面感覺聲音不夠好聽,于是使用了第三方
APP初稿如圖:

實(shí)現(xiàn)
1.初始化語音。這是一個(gè)異步操作。初始化完成后調(diào)用oninitListener(第二個(gè)參數(shù))。
TextToSpeech mTts = new TextToSpeech(this, this);
2.實(shí)現(xiàn)TextToSpeech.OnInitListener
注意:語言可能不可用。
// 實(shí)現(xiàn)TextToSpeech.OnInitListener.
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
//設(shè)置首選語言為中文,注意,語言可能是不可用的,結(jié)果將指示此
int result = mTts.setLanguage(Locale.CHINA);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
//語言數(shù)據(jù)丟失或不支持該語言。
Log.e(TAG, "語言數(shù)據(jù)丟失或不支持該語言");
} else {
//檢查文檔中其他可能的結(jié)果代碼。
// 例如,語言可能對區(qū)域設(shè)置可用,但對指定的國家和變體不可用
// TTS引擎已成功初始化。
// 允許用戶按下按鈕讓應(yīng)用程序再次發(fā)言。
mAgainButton.setEnabled(true);
}
} else {
// 初始化失敗
Log.e(TAG, "初始化失敗");
}
}
3.寫一個(gè)朗讀方法,在需要的時(shí)候觸發(fā)(如:點(diǎn)擊事件)
TextToSpeech的speak方法有兩個(gè)重載。
執(zhí)行朗讀的方法
speak(CharSequence text,int queueMode,Bundle params,String utteranceId);
第二個(gè)參數(shù)queueMode用于指定發(fā)音隊(duì)列模式,兩種模式選擇。
(1)TextToSpeech.QUEUE_FLUSH:該模式下在有新任務(wù)時(shí)候會(huì)清除當(dāng)前語音任務(wù),執(zhí)行新的語音任務(wù)
(2)TextToSpeech.QUEUE_ADD:該模式下會(huì)把新的語音任務(wù)放到語音任務(wù)之后,等前面的語音任務(wù)執(zhí)行完了才會(huì)執(zhí)行新的語音任務(wù)。
將朗讀的的聲音記錄成音頻文件
synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
private void sayHello() {
String hello ="Hellow";
//TextToSpeech的speak方法有兩個(gè)重載。
// 執(zhí)行朗讀的方法
//speak(CharSequence text,int queueMode,Bundle params,String utteranceId);
// 將朗讀的的聲音記錄成音頻文件
//synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
//第二個(gè)參數(shù)queueMode用于指定發(fā)音隊(duì)列模式,兩種模式選擇
//(1)TextToSpeech.QUEUE_FLUSH:該模式下在有新任務(wù)時(shí)候會(huì)清除當(dāng)前語音任務(wù),執(zhí)行新的語音任務(wù)
//(2)TextToSpeech.QUEUE_ADD:該模式下會(huì)把新的語音任務(wù)放到語音任務(wù)之后,
//等前面的語音任務(wù)執(zhí)行完了才會(huì)執(zhí)行新的語音任務(wù)
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH,
null);
}
4.記得利用Activity的生命周期中將其關(guān)閉
@Override
public void onDestroy() {
// 生命周期中結(jié)束
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
源碼
SpeechActivity.java
public class SpeechActivity extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "SpeechDemo";
private TextToSpeech mTts;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_to_speech);
//初始化語音。這是一個(gè)異步操作。初始化完成后調(diào)用oninitListener(第二個(gè)參數(shù))。
mTts = new TextToSpeech(this, this);
mButton = (Button) findViewById(R.id.again_button);
//觸發(fā)
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sayHello();
}
});
}
@Override
public void onDestroy() {
// 生命周期中結(jié)束
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
// 實(shí)現(xiàn)TextToSpeech.OnInitListener.
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
//設(shè)置首選語言為中文,注意,語言可能是不可用的,結(jié)果將指示此
int result = mTts.setLanguage(Locale.CHINA);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
//語言數(shù)據(jù)丟失或不支持該語言。
Log.e(TAG, "語言數(shù)據(jù)丟失或不支持該語言");
} else {
//檢查文檔中其他可能的結(jié)果代碼。
// 例如,語言可能對區(qū)域設(shè)置可用,但對指定的國家和變體不可用
// TTS引擎已成功初始化。
// 允許用戶按下按鈕讓應(yīng)用程序再次發(fā)言。
mAgainButton.setEnabled(true);
}
} else {
// 初始化失敗
Log.e(TAG, "初始化失敗");
}
}
private void sayHello() {
String hello ="計(jì)蒙不吃魚";
//TextToSpeech的speak方法有兩個(gè)重載。
// 執(zhí)行朗讀的方法
//speak(CharSequence text,int queueMode,Bundle params,String utteranceId);
// 將朗讀的的聲音記錄成音頻文件
//synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
//第二個(gè)參數(shù)queueMode用于指定發(fā)音隊(duì)列模式,兩種模式選擇
//(1)TextToSpeech.QUEUE_FLUSH:該模式下在有新任務(wù)時(shí)候會(huì)清除當(dāng)前語音任務(wù),執(zhí)行新的語音任務(wù)
//(2)TextToSpeech.QUEUE_ADD:該模式下會(huì)把新的語音任務(wù)放到語音任務(wù)之后,
//等前面的語音任務(wù)執(zhí)行完了才會(huì)執(zhí)行新的語音任務(wù)
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH,
null);
}
}
text_to_speech.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button android:id="@+id/again_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false" />
</LinearLayout>
到此這篇關(guān)于Android中TextToSpeech的使用的文章就介紹到這了,更多相關(guān)adroid TextToSpeech使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 如何定制vibrator的各種震動(dòng)模式M 具體方法
這篇文章介紹了Android 如何定制vibrator的各種震動(dòng)模式M 具體方法,有需要的朋友可以參考一下2013-09-09
設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn)
這篇文章主要介紹了設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn),需要的朋友可以參考下2014-07-07
Android實(shí)現(xiàn)圖片在屏幕內(nèi)縮放和移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控制圖片在屏幕內(nèi)縮放和移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android開發(fā)自學(xué)筆記(二):工程文件剖析
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(二):工程文件剖析,本文講解了AndroidManifest.xml、src文件夾、res文件夾等文件的作用,需要的朋友可以參考下2015-04-04
Android自定義加載loading view動(dòng)畫組件
這篇文章主要為大家詳細(xì)介紹了Android自定義加載loading view動(dòng)畫組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android實(shí)現(xiàn)新浪微博一鍵分享的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)新浪微博一鍵分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android中解決RecyclerView各種點(diǎn)擊事件的方法
這篇文章主要介紹了Android中解決RecyclerView各種點(diǎn)擊事件的方法,完美解決RecyclerView點(diǎn)擊事件、長按事件、子項(xiàng)點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
kotlin項(xiàng)目加入Glide圖片加載庫并使用GlideApp的方法
這篇文章主要給大家介紹了關(guān)于kotlin項(xiàng)目加入Glide圖片加載庫并使用GlideApp的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01

