android原生rom,本自带tts语音,在这基础上可以直接调用使用即可。源码如下。
public class TTSdemo extends Activity implements OnInitListener{
/** Called when the activity is first created. */ private EditText inputText = null; private Button speakBtn = null; /** * default state : CHECK_VOICE_DATA_FAIL = 0; */ private static final int REQ_TTS_STATUS_CHECK = 0; private static final String TAG = "TTS Demo"; private TextToSpeech mTts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try {//"这里注意,如果本机tts没有安装,这里就会抓获到异常的。tts使用的时候也要注意非空验证" // 检查TTS数据是否已经安装并且可用 Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);// 检查TTS状态 PackageManager pm = getPackageManager(); ResolveInfo resolveInfo = pm.resolveActivity( checkIntent, PackageManager.MATCH_DEFAULT_ONLY ); if( resolveInfo == null ) { // Not able to find the activity which should be started for this intent Log.e("----resolveInfo--------", "" + " Not able to find the activity which should be started for this intent"); } else { startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); } } catch (ActivityNotFoundException e) { Log.e(TAG, "Oops! The function is not available in your device." + e.fillInStackTrace()); } inputText = (EditText) findViewById(R.id.inputText); speakBtn = (Button) findViewById(R.id.speakBtn); inputText.setText("This is an example of speech synthesis."); speakBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mTts.speak(inputText.getText().toString(), TextToSpeech.QUEUE_ADD, null); // 朗读输入框里的内容 } }); }
// 实现TTS初始化接口 @Override public void onInit(int status) { // TODO Auto-generated method stub // TTS Engine初始化完成 if (status == TextToSpeech.SUCCESS) { int result = mTts.setLanguage(Locale.US); // 设置发音语言 if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) // 判断语言是否可用 { Log.v(TAG, "Language is not available"); speakBtn.setEnabled(false); } else { mTts.speak("This is an example of speech synthesis.", TextToSpeech.QUEUE_ADD, null); speakBtn.setEnabled(true); } } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQ_TTS_STATUS_CHECK) { switch (resultCode) { case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: // 这个返回结果表明TTS Engine可以用 { mTts = new TextToSpeech(this, this); Log.v(TAG, "TTS Engine is installed!"); } break; case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: // 需要的语音数据已损坏 case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: // 缺少需要语言的语音数据 case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: // 缺少需要语言的发音数据 { // 这三种情况都表明数据有错,重新下载安装需要的数据 Log.v(TAG, "Need language stuff:" + resultCode); Intent dataIntent = new Intent(); dataIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(dataIntent); } break; case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: // 检查失败 default: Log.v(TAG, "Got a failure. TTS apparently not available"); break; } } else { // 其他Intent返回的结果 } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (mTts != null) // activity暂停时也停止TTS { mTts.stop(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 释放TTS的资源 if (mTts != null) mTts.shutdown(); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/inputText" android:hint="@string/hint" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/speakBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
以上源码粘贴进入项目就没有问题了。但是这只是英文。而且对于手机的tts依赖很高,如果手机没有装tts,那么这个程序时不起作用的,无法发音。
所有,第二种,从网络寻找 离线“语音合成”, 大家都在说什么 讯飞XX ,百度XX等等, 百度不知道。至少讯飞亲自测试了。 就是一个收费的垃圾??
根本没有免费,而且还是在线的。如下。
于是继续找寻,暂时找到了一个离线的 ‘语音合成’demo,支持中文语音,但是发现一个问题,对于单词都读成一个个字母了。 暂时没有调整。 项目如下。
诸如此类的大量文章,手机如果没有tts,都是无法运行的(http://blog.csdn.net/ichliebephone/article/details/6373184#java)
我找到的,离线语音包的demo现在分享出来,共同研究。 下载地址:http://download.csdn.net/detail/flyingsir_zw/9453049
后续会继续更新。