android自学之TextToSpeech实现文字向语音的转换
2013-04-14 10:24
686 查看
package com.example.texttospeechexcercise; import java.util.Locale; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.speech.tts.TextToSpeech; import android.text.StaticLayout; public class MainActivity extends Activity implements TextToSpeech.OnInitListener{ String[] words=new String[]{"hello","dog","cat","bye"}; //static final Random RANDOM=new Random(); private TextToSpeech tSpeech; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.button1); tSpeech=new TextToSpeech(getApplicationContext(), this); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub sayHello(); } }); } private void sayHello() { // TODO Auto-generated method stub int size=words.length; //返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。 //nextInt 的常规协定是,伪随机地生成并返回指定范围中的一个 int 值。所有可能的 n 个 int 值的生成概率(大致)相同。 int i=new Random().nextInt(size); String word=words[i]; tSpeech.speak(word, TextToSpeech.QUEUE_FLUSH, null); } @Override protected void onDestroy() { // TODO Auto-generated method stub if (tSpeech!=null) { tSpeech.stop(); tSpeech.shutdown(); } super.onDestroy(); } @Override public void onInit(int status) { // TODO Auto-generated method stub if (status==TextToSpeech.SUCCESS) { int result=tSpeech.setLanguage(Locale.US); if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText(getApplicationContext(), "Language is not available.",1000).show(); } else { button.setEnabled(true); sayHello(); } }else { Toast.makeText(getApplicationContext(), "init failed", 1000).show(); } } }
相关文章推荐
- android自学之TextToSpeech例子实现将文字转化为语音功能
- Android联合第三方实现使用TextToSpeech实现语音朗读的功能
- Android中的TextToSpeech 将文本转换成语音的简单使用。
- Android中使用TTS(TextToSpeech)将文字转为语音
- Android TTS 中文 文字转语音 使用TextToSpeech Svox
- Android TTS 中文 文字转语音 使用TextToSpeech Svox .
- Android TTS 中文 文字转语音 使用TextToSpeech Svox
- 使用安卓中的TextToSpeech控件实现朗读文字
- 安卓文字转语音——其实可以很简单——TextToSpeech用法解析
- Android TTS 英文或中文(要下载中文支持包)转语音 使用TextToSpeech
- Android TTS文本转语音TextToSpeech
- android 融云 + 科大讯飞 实现仿微信语音消息转换为文字
- 使用安卓中的TextToSpeech控件实现朗读文字
- Android TTS(TextToSpeech)语音输出与RecognizerIntent语音识别
- 文字合成语音TextToSpeech语音解决中文不能用问题
- Android:使用Speech To Text API进行语音到文本转换
- 使用Svox实现中文转语音TextToSpeech(TTS)
- 使用Svox实现中文转语音TextToSpeech(TTS)
- Text-to-speech 入门与进阶学习笔记(android)