Android提供了自动朗读支持。
- TextToSpeech(Context context, TextToSpeech.OnInitListener listener)
- setLanguage(Locale loc)。如果调用setLanguage(Locale loc)的返回值是 TextToSpeech.LANG_COUNTRY_AVAILABLE 则说明当前TTS系统可以支持所设置的语言、国家选项。
- speak(String text, int queueMode, HashMap<String,String> params)
- synthesizeToFile(String text, HashMap<String,String> params, String filename)
- TextToSpeech.QUEUE_FLUSH
- TextToSpeech.QUEUE_ADD
归纳起来,使用TextToSpeech引擎的步骤如下:
(1)创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功。
(2)设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项。
(3)调用speak或synthesizeToFile方法。
(4)关闭TTS,释放资源。
src\org\crazyit\io\Speech.java
package org.crazyit.io;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (c), 2001-2014, F.L
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author [email protected]
* @version 1.0
*/
public class Speech extends Activity
{
TextToSpeech tts;
EditText editText;
Button speech;
Button record;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化TextToSpeech对象
tts = new TextToSpeech(this, new OnInitListener()
{
@Override
public void onInit(int status)
{
// 如果装载TTS成功
if ( status == TextToSpeech.SUCCESS)
{
// 设置使用英式英语朗读
int result = tts.setLanguage(Locale.UK);
// 如果不支持所设置的语言
if ( result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(Speech.this
, "TTS暂时不支持这种语言的朗读。", Toast.LENGTH_LONG)
.show();
}
}
}
});
editText = (EditText) findViewById(R.id.txt);
speech = (Button) findViewById(R.id.speech);
record = (Button) findViewById(R.id.record);
speech.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 执行朗读
tts.speak(editText.getText().toString(),
TextToSpeech.QUEUE_ADD, null);
}
});
record.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 将朗读文本的音频记录到指定文件
tts.synthesizeToFile(editText.getText().toString()
, null, "/mnt/sdcard/sound.wav");
Toast.makeText(Speech.this, "声音记录成功!"
, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onDestroy()
{
// 关闭TextToSpeec对象
if ( tts != null)
{
tts.shutdown();
}
}
}
res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="5"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/speech"
/>
<Button
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/record"
/>
</LinearLayout>
</LinearLayout>
res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Speek!</string>
<string name="app_name">自动朗读</string>
<string name="speech">朗读</string>
<string name="record">记录声音</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.crazyit.io"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".Speech"
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>