
- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動 (Activities)
- Android - 服務 (Services)
- Android - 廣播接收器 (Broadcast Receivers)
- Android - 內容提供器 (Content Providers)
- Android - 碎片 (Fragments)
- Android - 意圖/過濾器 (Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 打電話
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊採集
- Android - AudioManager
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入動畫 (Loading Spinner)
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - ProgressBar
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件 (Widgets)
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - 文字轉語音
Android 允許您將文字轉換為語音。您不僅可以轉換文字,還可以用多種不同的語言朗讀文字。
Android 為此提供了 **TextToSpeech** 類。要使用此類,您需要例項化此類的物件並指定 **initListener**。其語法如下:
private EditText write; ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { } });
在此監聽器中,您需要指定 TextToSpeech 物件的屬性,例如其語言、音調等。語言可以透過呼叫 **setLanguage()** 方法設定。其語法如下:
ttobj.setLanguage(Locale.UK);
setLanguage 方法採用 Locale 物件作為引數。以下是部分可用語言環境:
序號 | 語言環境 |
---|---|
1 | 美國 (US) |
2 | 加拿大法語 (CANADA_FRENCH) |
3 | 德國 (GERMANY) |
4 | 義大利 (ITALY) |
5 | 日本 (JAPAN) |
6 | 中國 (CHINA) |
設定語言後,您可以呼叫該類的 **speak** 方法來朗讀文字。其語法如下:
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
除了 speak 方法外,TextToSpeech 類中還提供了一些其他方法。列舉如下:
序號 | 方法及描述 |
---|---|
1 |
addSpeech(String text, String filename) 此方法在文字字串和聲音檔案之間新增對映。 |
2 |
getLanguage() 此方法返回一個描述語言的 Locale 例項。 |
3 |
isSpeaking() 此方法檢查 TextToSpeech 引擎是否正在忙於朗讀。 |
4 |
setPitch(float pitch) 此方法設定 TextToSpeech 引擎的語音音調。 |
5 |
setSpeechRate(float speechRate) 此方法設定語音速率。 |
6 |
shutdown() 此方法釋放 TextToSpeech 引擎使用的資源。 |
7 |
stop() 此方法停止朗讀。 |
示例
以下示例演示了 TextToSpeech 類的用法。它建立了一個基本的應用程式,允許您設定寫入文字並朗讀它。
要嘗試此示例,您需要在實際裝置上執行。
步驟 | 描述 |
---|---|
1 | 您將使用 Android Studio 在 com.example.sairamkrishna.myapplication 包下建立一個 Android 應用程式。 |
2 | 修改 src/MainActivity.java 檔案以新增 TextToSpeech 程式碼。 |
3 | 修改佈局 XML 檔案 res/layout/activity_main.xml,如有需要,新增任何 GUI 元件。 |
4 | 執行應用程式,選擇正在執行的 Android 裝置,將應用程式安裝到裝置上並驗證結果。 |
以下是 **src/MainActivity.java** 的內容。
package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.util.Locale; import android.widget.Toast; public class MainActivity extends Activity { TextToSpeech t1; EditText ed1; Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1=(EditText)findViewById(R.id.editText); b1=(Button)findViewById(R.id.button); t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != TextToSpeech.ERROR) { t1.setLanguage(Locale.UK); } } }); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String toSpeak = ed1.getText().toString(); Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show(); t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); } }); } public void onPause(){ if(t1 !=null){ t1.stop(); t1.shutdown(); } super.onPause(); } }
以下是 **activity_main.xml** 的內容。
在以下程式碼中,**abc** 表示 tutorialspoint.com 的徽標。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:transitionGroup="true"> <TextView android:text="Text to Speech" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:theme="@style/Base.TextAppearance.AppCompat" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageView" android:layout_marginTop="46dp" android:hint="Enter Text" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textColor="#ff7aff10" android:textColorHint="#ffff23d1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text to Speech" android:id="@+id/button" android:layout_below="@+id/editText" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" /> </RelativeLayout>
以下是 **Strings.xml** 的內容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 **AndroidManifest.xml** 的內容。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" > <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行您的應用程式。我假設您已將實際的 Android 移動裝置連線到計算機。要從 Android Studio 執行應用程式,請開啟專案的某個活動檔案,然後單擊工具欄中的執行 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗,讓您選擇要在其中執行 Android 應用程式的位置。

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示以下螢幕。

現在只需在欄位中鍵入一些文字,然後單擊下面的文字轉語音按鈕。將會出現通知,並朗讀文字。如下圖所示:

現在鍵入其他內容,並使用不同的語言環境重複此步驟。您將再次聽到聲音。如下圖所示:
