android学习笔记53——自动朗读TTS
自動(dòng)朗讀TTS
android提供了自動(dòng)朗讀功能——其指的是支持可以對(duì)指定文本內(nèi)容進(jìn)行朗讀,從而發(fā)出聲音;
同時(shí)android的自動(dòng)朗讀支持還允許把文本對(duì)應(yīng)的音頻錄制成音頻文件,方便后續(xù)播放。
這種自動(dòng)朗讀支持的英文名稱為:TextToSpeech,檢測(cè)TTS.
TTS,可以在應(yīng)用程序中動(dòng)態(tài)地增加音頻輸出,從而提高用戶體驗(yàn)。
Android的自動(dòng)朗讀支持通過TextToSpeech完成,該類提供了如下一個(gè)構(gòu)造函數(shù):
==>TextToSpeeech(Content content,TextToSpeeck.OnInitListener listener),當(dāng)創(chuàng)建TextToSpeech對(duì)象時(shí),必須先提供一個(gè)OnInitListener監(jiān)聽器——該監(jiān)聽器負(fù)責(zé)監(jiān)聽TextToSpeech的初始化效果。
程序獲取TextToSpeeech對(duì)象后,可調(diào)用TextToSpeeech的setLanguage(Locale loc)方法設(shè)置該TTS發(fā)聲引擎使用的語音、國家選項(xiàng)。
注意:
如果調(diào)用setLanguage(Locale loc)的返回值是:TextToSpeech.LANG_COUNTRY_AVALABLE,說明當(dāng)前TTS系統(tǒng)可以支持所設(shè)置的語音、國家選項(xiàng)。
對(duì)TextToSpeech設(shè)置完成后,即可調(diào)用它的方法來朗讀文本了,具體方法可參考TextToSpeech的API文檔。
TextToSpeech類中最常用的兩個(gè)方法如下:
1.speak(String text,int queueMode,HashMap<String,String> params);
2.synthesizeToFile(String text,HashMap<String,String> params,String fileName);
以上兩個(gè)方法都是用于把text文字內(nèi)容轉(zhuǎn)換為音頻,區(qū)別只是speak方法是播放轉(zhuǎn)換的音頻,而synthesizeToFile是把轉(zhuǎn)換得到的音頻保存成聲音文件。
以上兩個(gè)方法中的params都用于指定聲音轉(zhuǎn)換時(shí)的參數(shù),speak()中的queueMode參數(shù)指定TTS的發(fā)音隊(duì)列模式,該模式支持如下兩個(gè)常量:
1.TextToSpeech.QUEUE_PLUSH:如果指定該模式,當(dāng)TTS調(diào)用speak()時(shí),它會(huì)中斷當(dāng)前實(shí)例正在運(yùn)行的任務(wù)(也可理解為清除當(dāng)前語音任務(wù),轉(zhuǎn)而執(zhí)行新的語音任務(wù))
2.TextToSpeech.QUEUE_ADD:如果指定該模式,當(dāng)TTS調(diào)用speak()時(shí),會(huì)把新的發(fā)音任務(wù)添加到當(dāng)前發(fā)音任務(wù)列隊(duì)之后——也就是等任務(wù)隊(duì)列中的發(fā)音任務(wù)執(zhí)行完成后再來執(zhí)行speak()指定的發(fā)音任務(wù)。
注意:
當(dāng)程序用完了TextToSpeech對(duì)象后,可以在Activity的OnDestory()中調(diào)用它的shutdown()來關(guān)閉TextToSpeech、釋放其所占用資源。
總結(jié):TextToSpeech使用步驟如下:
1.創(chuàng)建TextToSpeech對(duì)象,創(chuàng)建時(shí)傳入OnInitListener監(jiān)聽器監(jiān)聽創(chuàng)建是否成功;
2.設(shè)置TextToSpeech所使用語言、國家選項(xiàng),通過返回值判斷TTS是否支持該語言、國家選項(xiàng);
3.調(diào)用speak()或synthesizeToFile();
4.關(guān)閉TTS,回收資源。
實(shí)例如下:
布局文件==》 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><EditTextandroid:id="@+id/edit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="this is a dog" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/btnRecord"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="記錄聲音" /><Buttonandroid:id="@+id/btnRead"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="朗讀" /></LinearLayout></LinearLayout>代碼實(shí)現(xiàn)==> package com.example.mytts;import java.util.Locale;import android.os.Bundle; import android.app.Activity; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;public class MainActivity extends Activity {EditText edit;TextToSpeech tts;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);edit = (EditText) this.findViewById(R.id.edit);Button btnRecord = (Button) this.findViewById(R.id.btnRecord);Button btnRead = (Button) this.findViewById(R.id.btnRead);btnRecord.setOnClickListener(new MyButtonOnClick());btnRead.setOnClickListener(new MyButtonOnClick());tts = new TextToSpeech(this, new OnInitListener(){@Overridepublic void onInit(int status){// 如果裝載TTS引擎成功if (status == TextToSpeech.SUCCESS){// 設(shè)置使用美式英語朗讀int result = tts.setLanguage(Locale.US);// 如果不支持所設(shè)置的語言、國家if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE&& result != TextToSpeech.LANG_AVAILABLE){Toast.makeText(MainActivity.this, "TTS不支持本次設(shè)置語言、國家的朗讀", 5000).show();}}}});}private class MyButtonOnClick implements OnClickListener{@Overridepublic void onClick(View v){switch (v.getId()){case R.id.btnRecord:// 將朗讀文本的音頻記錄到指定文件tts.synthesizeToFile(edit.getText().toString(), null, "/mnt/sdcard/sound.wav");Toast.makeText(MainActivity.this, "聲音記錄成功", 5000).show();break;case R.id.btnRead:tts.speak(edit.getText().toString(), TextToSpeech.QUEUE_ADD, null);break;}}}@Overrideprotected void onDestroy(){// super.onDestroy();if (tts != null)tts.shutdown();}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/YYkun/p/5977316.html
總結(jié)
以上是生活随笔為你收集整理的android学习笔记53——自动朗读TTS的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【移动开发】安卓Lab2(01)
- 下一篇: 跟老杨学java系列(一)前传