Android TTS 实战二:全世界都知道我爱你
扫描二维码
随时随地手机看文章
1.简介
实战而建立在实战一的基础上,只加了一个 Spinner ,给上一个 Demo 增加语言选择功能,可以使用 TTS 引擎支持的English、French、German、Italian和Spanish这5大语言来进行相应的语音合成,并用这5种外语向她说出“我爱你”。让全世界都知道我爱你~-~wlh
2.实例
xml布局代码
Java代码
package com.example.androidttsdemo2; import java.util.Locale; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity implements OnInitListener { /** * Called when the activity is first created. */ private EditText inputText = null; private Button speakBtn = null; private static final int REQ_TTS_STATUS_CHECK = 0; private static final String TAG = "TTS Demo"; private TextToSpeech mTts; private Spinner langSelect = null; private String languages[] = {"English", "French", "German", "Italian", "Spanish"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //检查TTS数据是否已经安装并且可用 Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); 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); //朗读输入框里的内容 } }); langSelect = (Spinner) findViewById(R.id.langSelect); inputText.setText("I love you"); ArrayAdapteradapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, languages); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); langSelect.setAdapter(adapter); langSelect.setSelection(0); langSelect.setOnItemSelectedListener(new OnItemSelectedListener() { 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); } } public void onNothingSelected(AdapterView arg) { // TODO Auto-generated method stub } }); } //实现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的资源 mTts.shutdown(); } }
在选择不同的item时,为inputText设置不同语言“我爱你”对应的文本,并且为TTS实例设置语言。
为TTS引擎设置语言的API为
public int setLanguage (Locale loc);
使用Locale这个表示语言及国家区域的类来设置语言,目前支持English、French、German、Italian和Spanish这5大语言,返回的结果为
LANG_AVAILABLE, LANG_COUNTRY_AVAILABLE, LANG_COUNTRY_VAR_AVAILABLE, LANG_MISSING_DATA 和LANG_NOT_SUPPORTED。
其中LANG_MISSING_DATA 和LANG_NOT_SUPPORTED表示无法使用当前进行设置的语言,另外的几个表示设置的语言有效。