隨著行動裝置 App的日益普及,移動語音應用越來越受到使用者的重視和喜愛。在Android中使用語音引擎其實是很容易的事情,通過若干步驟就可以實現了,本文將指導使用者如何在Android SDK中使用語音引擎去建立簡單的應用。
在本教程中,將一步步直接教讀者如何建立語音應用,具體的代碼在文末有下載。
步驟一 建立Android工程
我們首先建立一個Android工程,選用的是Android 2。3以上SDK的。
步驟二 建立使用者介面
由於我們的例子很簡單,所以只需要往介面中增添若干控制項就可以了,我們開啟main。xml檔案,寫入如下代碼,增加了一個本文框和一個按鈕:
view source print?
| 01 |
<TextView android:id="@+id/intro" |
| 02 |
android:layout_width="fill_parent" |
| 03 |
android:layout_height="wrap_content" |
| 04 |
android:text="Enter some text:" |
| 06 |
<EditText android:id="@+id/enter" |
| 07 |
android:layout_width="fill_parent" |
| 08 |
android:layout_height="wrap_content" |
| 10 |
<Button android:id="@+id/speak" |
| 11 |
android:layout_width="wrap_content" |
| 12 |
android:layout_height="wrap_content" |
步驟三 編寫監聽事件
接下來,我們為按鈕添加事件處理,首先要獲得使用者輸入的文字,代碼如下:
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
public class SpeakingAndroid extends Activity implements OnClickListener
{
//create the Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button speakButton = (Button)findViewById(R.id.speak);
//監聽button事件
speakButton.setOnClickListener(this);
}
//onClick事件的響應代碼
public void onClick(View v) {
//獲得使用者輸入的文字
EditText enteredText = (EditText)findViewById(R.id.enter);
String words = enteredText.getText().toString();
//調用語音引擎的方法
speakWords(words);
}
}
步驟四 編寫相關的語音方法
首先,要匯入相關的語音引擎包,代碼如下:
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech。OnInitListener;
接下來,為了能系統先對運行語音引擎的準備工作進行檢查,因此先要對其運行前的資料進行就緒檢查,所以首先聲明一個成員變數,如下:
private int MY_DATA_CHECK_CODE = 0;
並且在oncreate方法中,編寫如下代碼:
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
以上代碼的目的為,建立一個Intent,去檢查TTS所需要的資料是否準備好,如果準備好的話,則調用onActivityResult方法,這個方法在下文中會提到。
步驟五 建立TTS的執行個體
在onActivityResult方法中,可以開始建立其TTS語音引擎的執行個體,代碼如下
[java]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
當TTS的資料就緒資料檢查完畢後,將調用這個方法,其中resultCode為TTS驗證是否通過的標記,如果通過驗證的話,則通過myTTS = new TextToSpeech(this, this);
建立一個語音TTS執行個體。如果沒能通過TTS準備就緒的安裝測試,則新聲明一個Intent,將使用者引導到這個安裝TTS引擎的介面中去。
步驟六 初始化引擎的資料
由於在本程式中實現了OnInitListener介面,因此可以在oninit方法中,對語音引擎進行初始化設定,比如設定語言等,首先應該匯入相關的類庫,如下:
import java.util.Locale;
在oninit方法中寫入如下代碼:
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed…", Toast.LENGTH_LONG).show();
}
}
這裡判斷如果TTS已經就緒成功,則調用setLanguage方法設定為使用對應的語言(這
裡是英文),如果判斷TTS不能準備就緒,則調用Toast的顯示方法告知使用者。
步驟七 讓TTS說話
在準備好以上步驟後,最後一步就是讓TTS開口說話了,在speakWords方法中,寫入如下代碼即可
private void speakWords(String speech) {
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
其中的speak方法中有很多參數可以選用,具體請參考Android文檔,這裡選用了
參數TextToSpeech.QUEUE_FLUSH,如果想讓TTS語音引擎連續不斷地朗讀,可以使用
myTTS.speak(speech, TextToSpeech.QUEUE_ADD, null)的參數。如果要求應用不再進行朗讀,則可以添加一句:
myTTS.shutdown();
但除非是明確使用者不在需要應用進行朗讀,否則如果要連續讓系統朗讀的話,則不應該添加這句代碼。