TextToSpeech 文本自动朗读

Android提供了自动朗读支持。

[list]
[*]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
[/list]

归纳起来,使用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 F.L@126.com
* @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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值