Android實(shí)現(xiàn)自動(dòng)朗讀功能(TTS)
前言: Android提供了自動(dòng)朗讀支持??梢詫?duì)指定文本內(nèi)容進(jìn)行朗讀,從而發(fā)生聲音;還允許把文本對(duì)應(yīng)的音頻錄制成音頻文件,方便以后播放。Android的自動(dòng)朗讀主要通過(guò)TextToSpeech來(lái)完成,構(gòu)造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);當(dāng)創(chuàng)建TextToSpeech對(duì)象時(shí),必須先提供一個(gè)OnInitListener監(jiān)聽(tīng)器——負(fù)責(zé)監(jiān)聽(tīng)TextToSpeech的初始化結(jié)果。
效果圖如下:

使用TextToSpeech的步驟如下:
1、創(chuàng)建TextToSpeech對(duì)象,創(chuàng)建時(shí)傳入OnInitListener監(jiān)聽(tīng)器監(jiān)聽(tīng)示范創(chuàng)建成功。
2、設(shè)置TextToSpeech所使用語(yǔ)言國(guó)家選項(xiàng),通過(guò)返回值判斷TTS是否支持該語(yǔ)言、國(guó)家選項(xiàng)。
3、調(diào)用speak()或synthesizeToFile方法。
4、關(guān)閉TTS,回收資源。
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/input_text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/speech"
android:text="Speech"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/record"
android:text="Record"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Activity文件
public class SpeechActivity extends AppCompatActivity {
private EditText input;
private Button speech,record;
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == textToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.CHINA);
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE){
Toast.makeText(SpeechActivity.this, "TTS暫時(shí)不支持這種語(yǔ)音的朗讀!",
Toast.LENGTH_SHORT).show();
}
}
}
});
input = (EditText) findViewById(R.id.input_text);
speech = (Button) findViewById(R.id.speech);
record = (Button) findViewById(R.id.record);
speech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSpeech.speak(input.getText().toString(),
TextToSpeech.QUEUE_ADD, null);
}
});
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inputText = input.getText().toString();
HashMap<String, String> myHashRender = new HashMap<>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
textToSpeech.synthesizeToFile(inputText, myHashRender,
"/mnt/sdcard/my_recorder_audios/sound.wav");
Toast.makeText(SpeechActivity.this, "聲音記錄成功。", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
if (textToSpeech != null)
textToSpeech.shutdown();
super.onDestroy();
}
}
這里我們使用的是中文,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根據(jù)自己的需求更改為其他支持的語(yǔ)言。
最后在AndroidManifest.xml中加入權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
總結(jié):通過(guò)使用Android提供的TTS,我們可以對(duì)指定文本內(nèi)容進(jìn)行朗讀,從而發(fā)生聲音;還允許把文本對(duì)應(yīng)的音頻錄制成音頻文件,保存到本地。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android利用websocket協(xié)議與服務(wù)器通信
這篇文章主要為大家詳細(xì)介紹了android利用websocket協(xié)議與服務(wù)器通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android編程自定義Notification實(shí)例分析
這篇文章主要介紹了Android編程自定義Notification的用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了自定義Notification的具體功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12
詳解Android TextView屬性ellipsize多行失效的解決思路
這篇文章主要介紹了Android TextView屬性ellipsize多行失效的解決思路,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12
Android開(kāi)發(fā)實(shí)現(xiàn)圓形圖片功能示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圓形圖片功能,涉及Android實(shí)現(xiàn)圓形圖片的界面布局與CirImageView組件相關(guān)使用操作技巧,需要的朋友可以參考下2019-04-04
Android原生TabLayout使用的超全解析(看這篇就夠了)
現(xiàn)在很多app都有頂部可左右切換的導(dǎo)航欄,并且還帶動(dòng)畫(huà)效果,要實(shí)現(xiàn)這種導(dǎo)航欄,可以使用Android原生的Tablayout也可以借助第三方框架實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于Android原生TabLayout使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
Android Call(打電話(huà))的基本知識(shí)詳解
本文主要介紹了Android Call(打電話(huà))的基本知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
android:descendantFocusability方法介紹
開(kāi)發(fā)中很常見(jiàn)的一個(gè)問(wèn)題,項(xiàng)目中的listview不僅僅是簡(jiǎn)單的文字,常常需要自己定義listview,問(wèn)題就出現(xiàn)了,可能會(huì)發(fā)生點(diǎn)擊每一個(gè)item的時(shí)候沒(méi)有反應(yīng),無(wú)法獲取的焦點(diǎn)2012-11-11
TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家?guī)?lái)一篇TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

