使用TextToSpeech 的步骤如下:
- 创建 TextToSpeech 对象,创建传入时的 TextToSpeech.OnInitListener() 监听器监听创建是否成功.
- 设置 TextToSpeech 所使用的语言,国家选项,通过返回值判断TTS是否支持该语言, 国家选项.
- 调用 speak() 或者 synthesizeToFile()方法.
- 关闭 TTS,释放资源.
package com.test.tts;
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 android.widget.Toast;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* 自动朗读
*/
public class SpeechActivity extends Activity {
TextToSpeech tts;
EditText etText;
Button btnSpeech, btnRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech);
//第一步 初始化 TextToSpeech对象
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//判断加载 TTS引擎是否加载成功
if (status == TextToSpeech.SUCCESS) {
//设置使用美式英语朗读
int result = tts.setLanguage(Locale.US);
//如果不支持所设置的语言
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
Toast.makeText(SpeechActivity.this, "TTS暂时不支持这种语言的朗读", Toast.LENGTH_SHORT).show();
}
}
}
});
etText = (EditText) findViewById(R.id.et_text);
btnSpeech = (Button) findViewById(R.id.speech);
btnRecord = (Button) findViewById(R.id.record);
//朗读事件监听
btnSpeech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//执行朗读
tts.speak(etText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
}
});
//记录监听事件
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tts != null && etText != null) {
Map<String,String> map = new HashMap<String, String>(); //这里的map只是笔者自己加的 防止报错
//将朗读文本的音频记录到指定文件
tts.synthesizeToFile(etText.getText().toString(), (HashMap<String, String>) map, "/mnt/sdcard/sound.wav");
}
Toast.makeText(SpeechActivity.this, "声音记录成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//关闭 TextToSpeech 对象
if (tts != null) {
tts.shutdown();
}
}
}
布局界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.test.tts.SpeechActivity">
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入文字进行朗读"
android:lines="3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朗读" />
<Button
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记录" />
</LinearLayout>
</LinearLayout>
本文介绍了如何在Android中使用TextToSpeech实现自动朗读功能,包括创建TextToSpeech对象,设置语言和国家选项,以及调用speak()或synthesizeToFile()方法。同时提到了在布局界面的应用。
3752

被折叠的 条评论
为什么被折叠?



