儘管所有的Android設備,都支持該語音功能, 但部分設備因為儲存空間有限,可能缺少該語言的資源文件. 所以應用程序應先檢查設備是否存在語音合成資源(TTS resources)與相應的意圖(intent).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/englishresult"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/chineseresult"
/>
</LinearLayout>
主代碼
package com.AndroidTTS;
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.widget.TextView;
public class AndroidTTS extends Activity implements OnInitListener{
final int MY_TTS_CHECK_CODE=1;
private TextToSpeech myTTS;
TextView englishResult, chineseResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
englishResult = (TextView)findViewById(R.id.englishresult);
chineseResult = (TextView)findViewById(R.id.chineseresult);
//Check if TextToSpeech Engine installed.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_TTS_CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == MY_TTS_CHECK_CODE){
if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
//TextToSpeech Engine installed.
myTTS = new TextToSpeech(this, this);
}else{
//TextToSpeech Engine NOT installed, request to install.
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
int isLanguageAvailable;
//Check if Language available
isLanguageAvailable = myTTS.isLanguageAvailable(Locale.ENGLISH);
englishResult.setText("ENGLISH: "+ getStringResult(isLanguageAvailable));
isLanguageAvailable = myTTS.isLanguageAvailable(Locale.CHINESE);
chineseResult.setText("CHINESE: "+ getStringResult(isLanguageAvailable));
}
private String getStringResult(int resultLanguageAvailable)
{
String strResult;
switch(resultLanguageAvailable){
case(TextToSpeech.LANG_AVAILABLE):
strResult = "LANG_AVAILABLE";
break;
case(TextToSpeech.LANG_COUNTRY_AVAILABLE):
strResult = "LANG_COUNTRY_AVAILABLE";
break;
case(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE):
strResult = "LANG_COUNTRY_VAR_AVAILABLE";
break;
case(TextToSpeech.LANG_MISSING_DATA):
strResult = "LANG_MISSING_DATA";
break;
case(TextToSpeech.LANG_NOT_SUPPORTED):
strResult = "LANG_NOT_SUPPORTED";
break;
default:
strResult = "err: Should not happen!";
break;
}
return strResult;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myTTS.shutdown();
}
}
相關文章: 究竟"Android"如何讀? Text-To-Speech (TTS)
相關文章: 為Android詞典加入英語發聲功能
沒有留言:
發佈留言