Android - 概述

Android - 文字转语音

Android允许您将文字转换为语音.您不仅可以转换它,还可以使用各种不同语言的语言.

Android为此目的提供了 TextToSpeech 类.要使用此类,您需要实例化此类的对象,并指定 initListener .它的语法在下面给出 :

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

在此侦听器中,您必须指定TextToSpeech对象的属性,例如其语言,音高e.t.c.可以通过调用 setLanguage()方法设置语言.它的语法在下面和下面给出;

ttobj.setLanguage(Locale.UK);

方法setLanguage将Locale对象作为参数.一些可用语言环境的列表在下面和下面给出;

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

设置完语言后,您可以拨打课程的发言方法来讲话.它的语法在下面给出 :

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

除了speak方法外,TextToSpeech类还有其他一些方法.它们列在下面和下面;

Sr.No方法&描述
1

addSpeech (字符串文本,字符串文件名)

此方法在文本字符串和声音文件之间添加映射.

2

getLanguage()

此方法返回描述语言的Locale实例.

3

isSpeaking()

此方法检查TextToSpeech引擎是否正忙着说话.

4

setPitch(float pitch)

此方法设置TextToSpeech引擎的语音音高.

5

setSpeechRate(float speechRate)

此方法设定语速.

6

shutdown()

此方法释放TextToSpeech eng使用的资源ine.

7

stop()

此方法停止讲话.

示例

下面的示例演示了TextToSpeech类的使用.它创建了一个基本应用程序,允许您设置写入文本并说出它.

要试验此示例,您需要在实际设备上运行它.

步骤描述
1您将使用Android studio在包下创建Android应用程序com.example.sairamkrishna.myapplication.
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 表示it1352的标识.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工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏中.在开始你的应用程序之前,android studio将显示以下窗口,选择你想要运行你的Android应用程序的选项.

Anroid Text To Speech Tutorial

选择您的移动设备作为选项,然后检查您的移动设备,该设备将显示以下屏幕.

Android文字到语音教程

现在只需在字段中输入一些文字,然后点击下面的文字转语音按钮.将出现通知并说出文本.它显示在下方和下方的图像中;

Android文本到语音教程

现在输入其他内容并使用不同的区域设置再次重复该步骤.你会再次听到声音.这显示在下面 :

Android Text To Speech Tutorial