package com.example.ttsdemo;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/*自动朗诵(TTS)
* Android提供了自动朗读支持。自动朗读支持可以对指定文本内容进行朗读,从而发生声音;不仅如此,Android的自动朗读支持还
* 允许把文本对应的音频录制成音频文件,方便以后播放。这种自动朗读支持的英文名称为TextToSpeech,简称TTS
* 借助于TTS的支持,可以再应用程序中动态地增加音频输出,从而改善用户体验
* Android的自动朗读支持主要通过TextToSpeech来完成,该类提供了如下一个构造器:
* TextToSpeech(Context context,TextToSpeech.OnInitListener listener);
* 从上面的构造器不难看出,当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器--该监听器负责
* 监听TextToSpeech的初始化结果
* 一旦在程序中获得了TextToSpeech对象之后,接下来可调用TextToSpeech的setLanguage(Locale loc)方法来
* 设置该TTS发声引擎使用的语言、国家选项
* 如果调用setLanguage(Loacle loc)的返回值是“TextToSpeech.LANG_COUNTRY_AVAILABLE”说明当前TTS系统
* 可以支持所设置的语言、国家选项
* 对TextToSpeech设置完成后,就可调用它的方法来朗读文本了,TextToSpeech类中最常用的方法是如下两个
* speak(String text,int queueMode,HashMap<String,String>params)
* synthesizeToFile(String text,HashMap<String,String>params,String filename)
* 上面两个方法都用于把text文字内容转换为音频,区别是speak方法是播放转换的的音频,而synthesizeToFile
* 是把转换得到的音频保存成声音文件
* 上面两个方法中的params都用于指定声音转换时的参数,speak()方法中的queuMode参数指定TTS的发音队列模式,
* 该参数支持如下两个常量:
* TextToSpeech.QUEUE_FLUSH:如果指定该模式,当TTS调用speak方法时,它会中断当前实例正在运行的任务(也可
* 理解为清楚当前语音任务,转而执行新的语音任务)
* TextToSpeech.QUEUE_ADD:如果指定为该模式,当TTS调用speak方法时,会把新的发音任务添加到当前发音任务队列
* 之后--也就是等任务队列中的发音任务完成后再来执行speak()方法指定的发音任务
* 当程序用完TextToSpeech对象之后,可以在Activity的OnDestroy()方法中调用它的shutdown()来关闭TextToSpeech、
* 释放它所占用的资源
* 主要步骤:
* 1.创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功
* 2.设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项
* 3.调用speak()或synthesizeToFile方法
* 4.关闭TTS,回收资源
* 必须增加SD的读写权限:<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
*/
public class MainActivity extends Activity {
Button speak,record;
EditText edit;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speak=(Button) findViewById(R.id.spreak);
record=(Button) findViewById(R.id.record);
edit=(EditText) findViewById(R.id.edit);
/*初始化TextToSpeech对象*/
tts=new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
/*如果装载TTS引擎成功*/
if(status==TextToSpeech.SUCCESS){
/*设置使用某种语言朗读*/
int result=tts.setLanguage(Locale.ENGLISH);
/*如果不支持这种语言*/
if(result!=TextToSpeech.LANG_COUNTRY_AVAILABLE&&result!=TextToSpeech.LANG_AVAILABLE){
Toast.makeText(MainActivity.this,"TTS暂不支持这种语言的朗读", Toast.LENGTH_SHORT).show();
}
}else{/*没有TTS引擎*/
Toast.makeText(MainActivity.this,"暂不支持TTS", Toast.LENGTH_SHORT).show();
}
}
});
speak.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*执行朗读*/
tts.speak(edit.getText().toString(),TextToSpeech.QUEUE_ADD,null);
}
});
record.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*将朗诵文本的音频记录到指定文件*/
tts.synthesizeToFile(edit.getText().toString(), null,"/mnt/sdcard/sound.wav");
Toast.makeText(MainActivity.this, "声音记录成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(tts!=null){
/*关闭TestToSpeech*/
tts.shutdown();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spreak"
android:text="播放"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/record"
android:text="保存"
android:layout_weight="1"/>
</LinearLayout>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:gravity="top|left"
/>
</LinearLayout>