Android TTS技术《转》
Android TTS技术,也就是Text-to-speech技术。就是通过文字转化与语音的能力,而Android手机平台,也绑定了英文语音引擎,对英文语音提拱了内置的支持。通过输入一段英文,就可以将其以语音的形式播放出来。这也是该平台下的一个创意设计。
下面我们来看看这是如何实现的:
第一步:检查TTS数据是否可用:
java代码:
复制代码
复制代码
复制代码
复制代码
下面我们来看看这是如何实现的:
第一步:检查TTS数据是否可用:
java代码:
- //检查TTS数据是否已经安装并且可用
- Intent checkIntent = new Intent();
- checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
- startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
- 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返回的结果
- }
- }
下一步,初始化TTS。
java代码:
- //实现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);
- }
- }
- }
接下来,设置发音语言
java代码:
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- // TODO Auto-generated method stub
- int pos = langSelect.getSelectedItemPosition();
- int result = -1;
- switch (pos) {
- case 0:
- {
- inputText.setText("I love you");
- result = mTts.setLanguage(Locale.US);
- }
- break;
- case 1:
- {
- inputText.setText("Je t'aime");
- result = mTts.setLanguage(Locale.FRENCH);
- }
- break;
- case 2:
- {
- inputText.setText("Ich liebe dich");
- result = mTts.setLanguage(Locale.GERMAN);
- }
- break;
- case 3:
- {
- inputText.setText("Ti amo");
- result = mTts.setLanguage(Locale.ITALIAN);
- }
- break;
- case 4:
- {
- inputText.setText("Te quiero");
- result = mTts.setLanguage(new Locale("spa", "ESP"));
- }
- break;
- default:
- break;
- }
- //设置发音语言
- if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
- //判断语言是否可用
- {
- Log.v(TAG, "Language is not available");
- speakBtn.setEnabled(false);
- }
- else
- {
- speakBtn.setEnabled(true);
- }
- }
最近点击Button按钮发音:
java代码:
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mTts.speak(inputText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
- //朗读输入框里的内容
- }
posted on 2011-05-30 21:50 android爱好者 阅读(336) 评论(0) 编辑 收藏 举报