使用TextToSpeech实现文本转音频(自动朗读)
2015-04-08 21:12
471 查看
主要方法
setLanguage:设置语言的类型
speak:传入文本播放声音
synthesizeToFile:传入文本保存为音频
shutdown:释放TextToSpeech资源
setLanguage:设置语言的类型
speak:传入文本播放声音
synthesizeToFile:传入文本保存为音频
shutdown:释放TextToSpeech资源
package prictise.lxm.prictise; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Locale; /** * 使用TextToSpeech实现自动朗读 */ public class MainActivity extends Activity{ TextToSpeech textToSpeech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取界面视图 final EditText edTxtSpeak = (EditText)findViewById(R.id.edTxt_speak); final Button btnSpeak = (Button)findViewById(R.id.btn_speak); final Button btnSave = (Button)findViewById(R.id.btn_save); textToSpeech = new TextToSpeech(this,new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { //初始化成功,设置语言 if(status == TextToSpeech.SUCCESS){ //设置语言为美式英语 int result = textToSpeech.setLanguage(Locale.US); //设置语言为中文 // int result = textToSpeech.setLanguage(Locale.CHINA); if(result != TextToSpeech.LANG_AVAILABLE && result != TextToSpeech.LANG_COUNTRY_AVAILABLE){ //不支持当前语言 Toast.makeText(MainActivity.this,"不支持" + textToSpeech.getLanguage(). getDisplayName(),Toast.LENGTH_SHORT).show(); //设置发音按钮不可用 btnSave.setEnabled(false); btnSpeak.setEnabled(false); } } } }); //播放按钮 btnSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textToSpeech.speak(edTxtSpeak.getText().toString(),TextToSpeech.QUEUE_ADD,null); } }); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textToSpeech.synthesizeToFile(edTxtSpeak.getText().toString(),null,"speakUs"); } }); } protected void onDestroy() { //释放tts if(textToSpeech != null) { textToSpeech.shutdown(); } super.onDestroy(); } }
相关文章推荐
- TextToSpeech自动朗读使用
- 使用安卓中的TextToSpeech控件实现朗读文字
- 使用安卓中的TextToSpeech控件实现朗读文字
- Android联合第三方实现使用TextToSpeech实现语音朗读的功能
- Android应用开发提高篇(2)-----文本朗读TTS(TextToSpeech)
- TextToSpeech将文本转为语音的简单使用
- Android TextToSpeech多国语言自动朗读
- TTS(TextToSpeech)将文本转为语音的简单使用
- TextToSpeech将文本转为语音的简单使用
- C# 使用DotNetSpeech.dll实现文本朗读
- 使用TextToSpeech朗读文字
- 使用Svox实现中文转语音TextToSpeech(TTS)
- 利用微软Text-To-Speech朗读文本
- 使用Svox实现中文转语音TextToSpeech(TTS)
- speech 的使用 读文本发音(2) text to wav
- Android中的TextToSpeech 将文本转换成语音的简单使用。
- Android应用开发提高篇(2)-----文本朗读TTS(TextToSpeech)
- 使用ios7中的tts(text to speech)功能
- Android TTS 中文 文字转语音 使用TextToSpeech Svox
- jQuery中使用Ajax实现文本输入框的自动完成功能