http://code.google.com/p/google-api-translate-java/downloads/list
Eclipse >Package Explorer 中選取開發的專案 > Propertires > Java Build Path > Libabries > Add External JARs > 選到剛下載的.jar位置
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class TestPage extends Activity implements OnInitListener
{
private TextToSpeech mTts;
@Override
public void onCreate(Bundle savedInstanceState)
{
//檢查TTS數據是否已經安裝並且可用
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
}
@Override
public void onInit(int status) {
//TTS Engine初始化完成
if(status == TextToSpeech.SUCCESS)
{
int result = mTts.setLanguage(Locale.US); // 語言
mTts.setPitch(1.0f); // 音調
mTts.setSpeechRate(1); // 速度
//設置發音語言
//判斷語言是否可用
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//無法使用
Log.d("522", "Language is not available");
} else {
//可以使用, 這才去呼叫我們自己寫的function
refreshQustion();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQ_TTS_STATUS_CHECK)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) // 如果TTS Engine有成功找到的話
{
mTts = new TextToSpeech(this, this);
// 宣告一個 TextToSpeech instance, 註冊android.speech.tts.TextToSpeech.OnInitListener
// 當TTS Engine 初始完後會呼叫 onInit(int status)
Log.d("onActivityResult", "onInit");
}else{
// 如果 TTS 沒有安裝的話 , 要求安裝
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
protected void onPause() {
super.onPause();
if(mTts != null) {
//activity暫停時也停止TTS
mTts.stop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTts != null)
{
mTts.stop();
//釋放TTS的資源
mTts.shutdown();
}
}
public void refreshQustion() {
mTts.speak("text", TextToSpeech.QUEUE_FLUSH, null);
//TextToSpeech.QUEUE_ADD 為目前的念完才念
mTts.speak("tv_question.getText().toString()", TextToSpeech.QUEUE_ADD, null);
}
}
沒有留言:
張貼留言