Android TTS技术,也就是Text-to-speech技术。TTS技术对文本文件进行实时转换,转换时间之短可以秒计算。在其特有智能语音控制器作用下,文本输出的语音音律流畅,使得听者在听取信息时感觉自然,毫无机器语音输出的冷漠与生涩感。TTS语音合成技术即将覆盖国标一、二级汉字,具有英文接口,自动识别中、英文,支持中英文混读。所有声音采用真人普通话为标准发音,实现了120-150个汉字/秒的快速语音合成,朗读速度达3-4个汉字/秒,使用户可以听到清晰悦耳的音质和连贯流畅的语调。
下面我们来看看这是如何实现的:
***步:检查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);
- //朗读输入框里的内容
- }
【编辑推荐】