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() 此方法返回一个描述语言环境的实例。 |
| 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的logo
<?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 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的 Run
图标。在启动您的应用程序之前,Android Studio 将显示以下窗口以选择您要运行 Android 应用程序的选项。
选择您的移动设备作为选项,然后检查将显示以下屏幕的移动设备。
现在只需在字段中输入一些文本,然后单击下面的文本到语音按钮。 将出现一条通知并说出文本。 如下图所示 −
现在输入其他内容并使用不同的语言环境再次重复该步骤。 您将再次听到声音。 如下所示 −

