class="article_content clearfix">
class="htmledit_views">
Android提供了自动朗读功能TTS(TextToSpeech),有些人用过一些听书软件,就是把文字念出来,听起来不错,不过TTS目前并不支持中文(难道又是一个鸡肋?)
API详解
http://wang-peng1.iteye.com/blog/572849
下面做个简单的例子
朗读出来是一个成年女性的声音,语速有些快,不知道这个能不能调节
class="language-java">package WangLi.IO.Speech;
import java.util.Locale;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Activity;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Bundle;
import class="tags" href="/tags/ANDROID.html" title=android>android.speech.class="tags" href="/tags/TTS.html" title=tts>tts.TextToSpeech;
import class="tags" href="/tags/ANDROID.html" title=android>android.speech.class="tags" href="/tags/TTS.html" title=tts>tts.TextToSpeech.OnInitListener;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.View;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.View.OnClickListener;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.Button;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.EditText;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.Toast;
public class Speech extends Activity {
TextToSpeech class="tags" href="/tags/TTS.html" title=tts>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对象
class="tags" href="/tags/TTS.html" title=tts>tts = new TextToSpeech(this,new OnInitListener()
{
@Override
public void onInit(int status) {
//如果装载TTS引擎成功
if(status == TextToSpeech.SUCCESS)
{
//设置使用美式英语朗读(虽然设置里有中文选项Locale.Chinese,但并不支持中文)
int result = class="tags" href="/tags/TTS.html" title=tts>tts.setLanguage(Locale.US);
//如果不支持设置的class="tags" href="/tags/YuYan.html" title=语言>语言
if(result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(Speech.this, "TTS暂时不支持这种class="tags" href="/tags/YuYan.html" title=语言>语言朗读", 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) {
//执行朗读
class="tags" href="/tags/TTS.html" title=tts>tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, class="tags" href="/tags/NULL.html" title=null>null);
}});
record.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//将朗读文本的音频记录到指定文件
class="tags" href="/tags/TTS.html" title=tts>tts.synthesizeToFile(editText.toString().toString(), class="tags" href="/tags/NULL.html" title=null>null, "/sdcard/sound.wav");
Toast.makeText(Speech.this, "声音记录成功", 50000).show();
}});
}
@Override
public void onDestroy()
{
//关闭TextToSpeech对象
if(class="tags" href="/tags/TTS.html" title=tts>tts != class="tags" href="/tags/NULL.html" title=null>null)
class="tags" href="/tags/TTS.html" title=tts>tts.shutdown();
}
}