基于Android TTS服務(wù)開(kāi)發(fā)智能助手
譯文簡(jiǎn)介
本文中將向你展示一個(gè)基本的Android應(yīng)用程序,此程序能夠聆聽(tīng)用戶的聲音并把它轉(zhuǎn)換為文本數(shù)據(jù)。而且,此程序還能夠進(jìn)行文本分析,然后執(zhí)行相應(yīng)的命令來(lái)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)及用戶應(yīng)答功能。
注意,本文源碼工程下載地址是:https://github.com/sitepoint-editors/SpeechApplication。
程序快照如下:
創(chuàng)建應(yīng)用程序
打開(kāi)Android Studio創(chuàng)建一個(gè)新的項(xiàng)目,選擇最小版本的Android API 18并添加一個(gè)空的Activity。這也是本項(xiàng)目中唯一的一個(gè)Activity。
為了實(shí)現(xiàn)視圖的全屏顯示,打開(kāi)配置文件AndroidManifest.xml,并設(shè)置如下:
- android:theme="@style/Theme.AppCompat.NoActionBar"
這個(gè)配置將使我們當(dāng)前的活動(dòng)(Activity)中隱藏ActionBar的顯示。
到此,你已經(jīng)擁有一個(gè)全屏式的白色背景色布局的視圖,其中僅有一個(gè)TextView控件。為了作一些改進(jìn),你可以把一個(gè)漸變形狀添加到RelativeLayout上。
接下來(lái),右擊drawable文件夾并選擇New->Drawable resource file。命名這個(gè)資源文件為background,并使用如下代碼替換原來(lái)的內(nèi)容:
- <?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>
實(shí)際上,你可以根據(jù)自己的喜歡任意地修改顏色與角度。
注意:布局中的ImageButton控件使用了一個(gè)來(lái)自于https://design.google.com/icons/#ic_mic_none網(wǎng)站提供的圖像。你可以下載并把它以資源方式添加使用。
接下來(lái),更新文件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>
增加說(shuō)話功能
現(xiàn)在,用戶接口部分已經(jīng)完成,接下來(lái)要編寫位于MainActivity內(nèi)部的Java代碼了。
首先,在onCreate方法上面聲明一個(gè)變量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!");
- }
- }
- });
上述代碼將啟動(dòng)系統(tǒng)中的TextToSpeech服務(wù)。其中的speak()方法使用了一個(gè)String類型的參數(shù),它是你要求你的Android設(shè)備需要念出的文字。
下面要?jiǎng)?chuàng)建這個(gè)方法,并添加如下代碼:
- 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檢查,因?yàn)閠ts.speak(param,param,param)格式的調(diào)用對(duì)于Android API 5.1來(lái)說(shuō)已經(jīng)廢棄了。
在speak()方法后面再創(chuàng)建另一個(gè)方法,用于當(dāng)用戶關(guān)閉程序時(shí)負(fù)責(zé)停止TextToSpeech服務(wù):
- @Override
- public void onDestroy() {
- if (tts != null) {
- tts.stop();
- tts.shutdown();
- }
- super.onDestroy();
- }
到此,一旦你啟動(dòng)程序,這個(gè)程序便能說(shuō)出“Hello”這樣的語(yǔ)句了。接下來(lái)要實(shí)現(xiàn)的是使程序具備聽(tīng)的功能。
增加聆聽(tīng)功能
為了使程序能夠具備聽(tīng)的功能,你需要使用麥克風(fēng)按鈕。為此,需要在onCreate方法中添加如下代碼:
- findViewById(R.id.microphoneButton).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listen();
- }
- });
點(diǎn)擊ImageButton控件時(shí),將觸發(fā)調(diào)用下面這個(gè)函數(shù):
- 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();
- }
- }
此方法將啟動(dòng)listening Activity,這個(gè)活動(dòng)會(huì)顯示一個(gè)帶有一段文本提示的對(duì)話框。講話所使用的語(yǔ)言是設(shè)備提供的,通過(guò)Locale.getDefault()方法實(shí)現(xiàn)。
StartActivityForResult (i,100)方法等待當(dāng)前活動(dòng)返回一個(gè)結(jié)果。100只是一個(gè)附加到已啟動(dòng)活動(dòng)的隨機(jī)代碼,其實(shí)也可以是一個(gè)任何適合你需要的數(shù)字。當(dāng)結(jié)果從已啟動(dòng)的活動(dòng)返回時(shí),它包含這個(gè)代碼并使用此代碼來(lái)區(qū)分來(lái)自于彼此的多個(gè)結(jié)果。
要從已啟動(dòng)的活動(dòng)中捕獲結(jié)果,需要添加下面的重寫方法:
- @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);
- }
- }
- }
這個(gè)方法捕獲來(lái)自于活動(dòng)的每一個(gè)結(jié)果并使用requestCode來(lái)處理語(yǔ)言識(shí)別器結(jié)果數(shù)據(jù)。如果requestCode等于100,requestCode等于OK并且來(lái)自于這個(gè)結(jié)果的數(shù)據(jù)不是null。你會(huì)從res.get(0)中取得result字符串。
接下來(lái),創(chuàng)建一個(gè)新方法recognition,此方法將使用一個(gè)String類型的參數(shù):
- private void recognition(String text){
- Log.e("Speech",""+text);
- }
到現(xiàn)在為止,當(dāng)用戶點(diǎn)擊麥克風(fēng)按鈕時(shí),程序能夠聽(tīng)聲音了,并且能夠把用戶的語(yǔ)言轉(zhuǎn)換為文本數(shù)據(jù),最終結(jié)果將通過(guò)Error日志打印輸出。
增加學(xué)習(xí)功能
為了使程序更有趣一些,在這一小節(jié)中你要使應(yīng)用程序能夠?qū)W習(xí)一些簡(jiǎn)單的事情,例如你的名字。為了實(shí)現(xiàn)這一功能,需要使用本地存儲(chǔ)功能。
首先,在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();
首先,你需要使應(yīng)用程序提問(wèn)問(wèn)題,所以需要把speak("Hello")修改為speak(“What is your name?)。
在這里,您可以使用一個(gè)簡(jiǎn)單的邏輯;所以,當(dāng)有人問(wèn)"你的名字是什么?",答案是"我的名字是Dori",于是從答案中提出名字。一個(gè)簡(jiǎn)單的方法是把答案拆分由空格分隔的字符串并獲取最后一個(gè)索引的值。
于是,我們要更新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方法使用來(lái)自用戶語(yǔ)音的所有結(jié)果。既然講話可能是不同的,你可以使用它們可能包含的某些單詞來(lái)區(qū)別它們。
例如,這個(gè)方法中的代碼可以是:
- 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));
- }
- }
但這仍然是一個(gè)與應(yīng)用程序的簡(jiǎn)單交互。你可以讓它學(xué)習(xí)你的年齡,或者給它起一個(gè)名字。
在同一個(gè)方法中,你可以嘗試下面這些簡(jiǎn)單的條件:
- //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.");
- }
應(yīng)用程序能夠告訴你時(shí)間信息:
- //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));
- }
小結(jié)
在我創(chuàng)建的GitHub工程(https://github.com/sitepoint-editors/SpeechApplication)中包含了更多的例子,你可以充分地進(jìn)行實(shí)驗(yàn)并開(kāi)發(fā)出你自己真正的Android助手程序。
最后,希望你喜歡這個(gè)教程,并希望你能夠與自己的手機(jī)產(chǎn)生一次真正有用的對(duì)話。