Android - 文本转语音
Android 允许您将文本转换为语音。不仅可以转换,还支持用多种不同语言朗读文本。
Android 提供了 TextToSpeech class 来实现此功能。要使用这个 class,您需要实例化该 class 的对象,并指定 initListener。其语法如下 −
private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
}
});
在这个 listener 中,您需要为 TextToSpeech 对象指定属性,例如语言、音调等。通过调用 setLanguage() 方法来设置语言。其语法如下 −
ttobj.setLanguage(Locale.UK);
setLanguage 方法接受一个 Locale 对象作为参数。以下是一些可用的 locale 列表 −
| 序号 | Locale |
|---|---|
| 1 | US |
| 2 | CANADA_FRENCH |
| 3 | GERMANY |
| 4 | ITALY |
| 5 | JAPAN |
| 6 | CHINA |
设置好语言后,您可以调用该 class 的 speak 方法来朗读文本。其语法如下 −
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
除了 speak 方法外,TextToSpeech class 还提供了其他一些方法。它们列在下面 −
| 序号 | 方法及描述 |
|---|---|
| 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 class 的使用。它创建一个基本应用,允许你输入文本并朗读。
要实验这个示例,你需要在实际设备上运行。
| 步骤 | 描述 |
|---|---|
| 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 表示 example.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=""
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 运行应用,打开项目的一个 activity 文件,然后点击工具栏中的运行
图标。在启动应用之前,Android studio 将显示以下窗口,让你选择要在哪里运行 Android 应用。
选择你的移动设备作为选项,然后检查你的移动设备,将显示以下屏幕。
现在只需在字段中输入一些文本,然后点击下面的文本转语音按钮。会出现一个通知,文本将被朗读。如下图所示 −
现在输入其他内容,并使用不同的 locale 重复此步骤。你将再次听到声音。如下图所示 −
