Android提供了自动朗读功能TTS(TextToSpeech),有些人用过一些听书软件,就是把文字念出来,听起来不错,不过TTS目前并不支持中文(难道又是一个鸡肋?)
API详解
http://wang-peng1.iteye.com/blog/572849
下面做个简单的例子
朗读出来是一个成年女性的声音,语速有些快,不知道这个能不能调节
package WangLi.IO.Speech;
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;
public class Speech extends Activity {
TextToSpeech tts;
EditText editText;
Button speech;
Button record;
/** Called when the activity is first created. */
@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)
{
//设置使用美式英语朗读(虽然设置里有中文选项Locale.Chinese,但并不支持中文)
int result = tts.setLanguage(Locale.US);
//如果不支持设置的语言
if(result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(Speech.this, "TTS暂时不支持这种语言朗读", 50000).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 v) {
//将朗读文本的音频记录到指定文件
tts.synthesizeToFile(editText.toString().toString(), null, "/sdcard/sound.wav");
Toast.makeText(Speech.this, "声音记录成功", 50000).show();
}});
}
@Override
public void onDestroy()
{
//关闭TextToSpeech对象
if(tts != null)
tts.shutdown();
}
}