基于Android TTS服務開發智能助手
譯文簡介
本文中將向你展示一個基本的Android應用程序,此程序能夠聆聽用戶的聲音并把它轉換為文本數據。而且,此程序還能夠進行文本分析,然后執行相應的命令來實現數據存儲及用戶應答功能。
注意,本文源碼工程下載地址是:https://github.com/sitepoint-editors/SpeechApplication。
程序快照如下:
創建應用程序
打開Android Studio創建一個新的項目,選擇最小版本的Android API 18并添加一個空的Activity。這也是本項目中唯一的一個Activity。
為了實現視圖的全屏顯示,打開配置文件AndroidManifest.xml,并設置如下:
- android:theme="@style/Theme.AppCompat.NoActionBar"
這個配置將使我們當前的活動(Activity)中隱藏ActionBar的顯示。
到此,你已經擁有一個全屏式的白色背景色布局的視圖,其中僅有一個TextView控件。為了作一些改進,你可以把一個漸變形狀添加到RelativeLayout上。
接下來,右擊drawable文件夾并選擇New->Drawable resource file。命名這個資源文件為background,并使用如下代碼替換原來的內容:
- <?xml version="1.0" encoding="UTF-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle" >
- <gradient
- android:type="linear"
- android:startColor="#FF85FBFF"
- android:endColor="#FF008080"
- android:angle="45"/>
- </shape>
實際上,你可以根據自己的喜歡任意地修改顏色與角度。
注意:布局中的ImageButton控件使用了一個來自于https://design.google.com/icons/#ic_mic_none網站提供的圖像。你可以下載并把它以資源方式添加使用。
接下來,更新文件activity_main.xml中的代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/background"
- android:id="@+id/rel"
- tools:context="com.example.theodhor.speechapplication.MainActivity">
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/microphoneButton"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:src="@drawable/ic_mic_none_white_48dp"
- android:background="@null"/>
- </RelativeLayout>
增加說話功能
現在,用戶接口部分已經完成,接下來要編寫位于MainActivity內部的Java代碼了。
首先,在onCreate方法上面聲明一個變量TextToSpeech:
- private TextToSpeechtts;
然后,在onCreate方法中添加如下代碼:
- tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
- @Override
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- int result = tts.setLanguage(Locale.US);
- if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
- Log.e("TTS", "This Language is not supported");
- }
- speak("Hello");
- } else {
- Log.e("TTS", "Initilization Failed!");
- }
- }
- });
上述代碼將啟動系統中的TextToSpeech服務。其中的speak()方法使用了一個String類型的參數,它是你要求你的Android設備需要念出的文字。
下面要創建這個方法,并添加如下代碼:
- private void speak(String text){
- if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
- tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
- }else{
- tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }
- }
上面代碼中使用了Build.VERSION檢查,因為tts.speak(param,param,param)格式的調用對于Android API 5.1來說已經廢棄了。
在speak()方法后面再創建另一個方法,用于當用戶關閉程序時負責停止TextToSpeech服務:
- @Override
- public void onDestroy() {
- if (tts != null) {
- tts.stop();
- tts.shutdown();
- }
- super.onDestroy();
- }
到此,一旦你啟動程序,這個程序便能說出“Hello”這樣的語句了。接下來要實現的是使程序具備聽的功能。
增加聆聽功能
為了使程序能夠具備聽的功能,你需要使用麥克風按鈕。為此,需要在onCreate方法中添加如下代碼:
- findViewById(R.id.microphoneButton).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listen();
- }
- });
點擊ImageButton控件時,將觸發調用下面這個函數:
- private void listen(){
- Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
- i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
- try {
- startActivityForResult(i, 100);
- } catch (ActivityNotFoundException a) {
- Toast.makeText(MainActivity.this, "Your device doesn't support Speech Recognition", Toast.LENGTH_SHORT).show();
- }
- }
此方法將啟動listening Activity,這個活動會顯示一個帶有一段文本提示的對話框。講話所使用的語言是設備提供的,通過Locale.getDefault()方法實現。
StartActivityForResult (i,100)方法等待當前活動返回一個結果。100只是一個附加到已啟動活動的隨機代碼,其實也可以是一個任何適合你需要的數字。當結果從已啟動的活動返回時,它包含這個代碼并使用此代碼來區分來自于彼此的多個結果。
要從已啟動的活動中捕獲結果,需要添加下面的重寫方法:
- @Override
- protected void onActivityResult(intrequestCode, intresultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == 100){
- if (resultCode == RESULT_OK &&null != data) {
- ArrayList<String> res = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- String inSpeech = res.get(0);
- recognition(inSpeech);
- }
- }
- }
這個方法捕獲來自于活動的每一個結果并使用requestCode來處理語言識別器結果數據。如果requestCode等于100,requestCode等于OK并且來自于這個結果的數據不是null。你會從res.get(0)中取得result字符串。
接下來,創建一個新方法recognition,此方法將使用一個String類型的參數:
- private void recognition(String text){
- Log.e("Speech",""+text);
- }
到現在為止,當用戶點擊麥克風按鈕時,程序能夠聽聲音了,并且能夠把用戶的語言轉換為文本數據,最終結果將通過Error日志打印輸出。
增加學習功能
為了使程序更有趣一些,在這一小節中你要使應用程序能夠學習一些簡單的事情,例如你的名字。為了實現這一功能,需要使用本地存儲功能。
首先,在onCreate方法中添加如下代碼:
- private SharedPreferences preferences;
- private SharedPreferences.Editor editor;
- private static final String PREFS = "prefs";
- private static final String NAME = "name";
- private static final String AGE = "age";
- private static final String AS_NAME = "as_name";
然后,在onCreate方法中添加如下代碼:
- preferences = getSharedPreferences(PREFS,0);
- editor = preferences.edit();
首先,你需要使應用程序提問問題,所以需要把speak("Hello")修改為speak(“What is your name?)。
在這里,您可以使用一個簡單的邏輯;所以,當有人問"你的名字是什么?",答案是"我的名字是Dori",于是從答案中提出名字。一個簡單的方法是把答案拆分由空格分隔的字符串并獲取最后一個索引的值。
于是,我們要更新recognition方法中的代碼,如下所示:
- private void recognition(String text){
- Log.e("Speech",""+text);
- //creating an array which contains the words of the answer
- String[] speech = text.split(" ");
- //the last word is our name
- String name = speech[speech.length-1];
- //we got the name, we can put it in local storage and save changes
- editor.putString(NAME,name).apply();
- //make the app tell our name
- speak("Your name is "+preferences.getString(NAME,null));
- }
recognition方法使用來自用戶語音的所有結果。既然講話可能是不同的,你可以使用它們可能包含的某些單詞來區別它們。
例如,這個方法中的代碼可以是:
- private void recognition(String text){
- Log.e("Speech",""+text);
- String[] speech = text.split(" ");
- //if the speech contains these words, the user is saying their name
- if(text.contains("my name is")){
- String name = speech[speech.length-1];
- Log.e("Your name", "" + name);
- editor.putString(NAME,name).apply();
- speak("Your name is "+preferences.getString(NAME,null));
- }
- }
但這仍然是一個與應用程序的簡單交互。你可以讓它學習你的年齡,或者給它起一個名字。
在同一個方法中,你可以嘗試下面這些簡單的條件:
- //This must be the age
- //Just speak: I am x years old.
- if(text.contains("years") &&text.contains("old")){
- String age = speech[speech.length-3];
- Log.e("THIS", "" + age);
- editor.putString(AGE, age).apply();
- }
- //Then ask it for your age
- if(text.contains("how old am I")){
- speak("You are "+preferences.getString(AGE,null)+" years old.");
- }
應用程序能夠告訴你時間信息:
- //Ask: What time is it?
- if(text.contains("what time is it")){
- SimpleDateFormatsdfDate = new SimpleDateFormat("HH:mm");//dd/MM/yyyy
- Date now = new Date();
- String[] strDate = sdfDate.format(now).split(":");
- if(strDate[1].contains("00"))strDate[1] = "o'clock";
- speak("The time is " + sdfDate.format(now));
- }
小結
在我創建的GitHub工程(https://github.com/sitepoint-editors/SpeechApplication)中包含了更多的例子,你可以充分地進行實驗并開發出你自己真正的Android助手程序。
最后,希望你喜歡這個教程,并希望你能夠與自己的手機產生一次真正有用的對話。