• Android Video Tutorials

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 執行應用程式,請開啟專案的某個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗,讓您選擇要在其中執行 Android 應用程式的位置。

Anroid Text To Speech Tutorial

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

Android Text To Speech Tutorial

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

Android Text To Speech Tutorial

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

Android Text To Speech Tutorial
廣告