Android-text-to-speech

提供:Dev Guides
移動先:案内検索

Android-テキスト読み上げ

Androidでは、テキストを音声に変換できます。 変換できるだけでなく、さまざまな言語でテキストを話すこともできます。

Androidは、この目的のために TextToSpeech クラスを提供します。 このクラスを使用するには、このクラスのオブジェクトをインスタンス化し、 initListener も指定する必要があります。 その構文は以下のとおりです-

private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
   @Override
   public void onInit(int status) {
   }
});

このリスナーでは、言語、ピッチe.t.cなど、TextToSpeechオブジェクトのプロパティを指定する必要があります。 言語を設定するには、* setLanguage()*メソッドを呼び出します。 その構文は以下のとおりです-

ttobj.setLanguage(Locale.UK);

メソッドsetLanguageは、パラメーターとしてLocaleオブジェクトを取ります。 利用可能なロケールのいくつかのリストは以下に与えられています-

Sr.No Locale
1 US
2 CANADA_FRENCH
3 GERMANY
4 ITALY
5 JAPAN
6 CHINA

言語を設定したら、クラスの speak メソッドを呼び出してテキストを話すことができます。 その構文は以下のとおりです-

ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

speakメソッドとは別に、TextToSpeechクラスで使用できる他のメソッドがいくつかあります。 それらは以下にリストされています-

Sr.No Method & description
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クラスの使用方法を示しています。 テキストを書いてそれを話すことができる基本的なアプリケーションを作成します。

この例を試すには、実際のデバイスでこれを実行する必要があります。

Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add TextToSpeech code.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Run the application and choose a running android device and install the application on it and verify the results.
*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 *はfinddevguides.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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Android StudioはAndroidアプリケーションを実行するオプションを選択する次のウィンドウを表示します。

音声による音声入力のチュートリアル

オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します。

Android Text To Speechチュートリアル

次に、フィールドにテキストを入力し、下のテキスト読み上げボタンをクリックします。 通知が表示され、テキストが読み上げられます。 それは以下の画像に示されています-

Android Text To Speechチュートリアル

次に、別の何かを入力して、別のロケールでステップを繰り返します。 再び音が聞こえます。 これは以下に示されています-

Android Text To Speechチュートリアル