Demo程序
這個程序很簡單。他有一個Mic符號按鈕。點擊之后我們觸發Android的Speech To Text意圖(Intent)顯示一個對話框來接收語音輸入。輸入的語音然后會被轉換成文本並顯示到一個text view中。
第一步:在Eclipse中創建基本的Android項目
在Eclipse中創建一個Hello World Android項目。打開
New > Project > Android Project,項目名填
SpeechToTextDemo,選擇Android運行時2.1或sdk7。我給定了包名:
net.viralpatel.android.speechtotextdemo
做完上面的步驟,你就有了一個基本的Android Hello World程序
第二步:更改布局
在我們的demo中布局很簡單。只有一個圖像按鈕來觸發Speech to Text API和一個TextView來顯示從語音轉換過來的文本。
打開layout/main.xml並替換為下面的內容:
File: res/layout/main.xml
01 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
02 |
xmlns:tools="http://schemas.android.com/tools" |
03 |
android:layout_width="fill_parent" |
04 |
android:layout_height="wrap_content" |
05 |
android:layout_above="@+id/textView1" |
06 |
android:layout_toLeftOf="@+id/textView1" |
07 |
android:gravity="center" |
08 |
android:orientation="vertical" > |
09 |
|
10 |
<ImageButton |
11 |
android:id="@+id/btnSpeak" |
12 |
android:layout_width="fill_parent" |
13 |
android:layout_height="wrap_content" |
14 |
android:layout_margin="10dp" |
15 |
android:layout_marginRight="10dp" |
16 |
android:layout_marginTop="10dp" |
17 |
android:contentDescription="@string/speak" |
18 |
android:src="<a href="http://my.oschina.net/asia" class="referer"target="_blank">@android</a> :drawable/ic_btn_speak_now" /> |
19 |
|
20 |
<TextView |
21 |
android:id="@+id/txtText" |
22 |
android:layout_width="wrap_content" |
23 |
android:layout_height="wrap_content" |
24 |
android:layout_marginLeft="10dp" |
25 |
android:layout_marginRight="10dp" |
26 |
android:layout_marginTop="10dp" |
27 |
android:textAppearance="?android:attr/textAppearanceLarge" /> |
28 |
|
29 |
</LinearLayout> |
第三步:觸發Speech to Text API的Android Java代碼
打開SpeechToTextDemoActivity 類並替換為下面的代碼:
File: SpeechToTextDemoActivity.java
01 |
package net.viralpatel.android.speechtotextdemo; |
02 |
|
03 |
import java.util.ArrayList; |
04 |
|
05 |
import android.app.Activity; |
06 |
import android.content.ActivityNotFoundException; |
07 |
import android.content.Intent; |
08 |
import android.os.Bundle; |
09 |
import android.speech.RecognizerIntent; |
10 |
import android.view.Menu; |
11 |
import android.view.View; |
12 |
import android.widget.ImageButton; |
13 |
import android.widget.TextView; |
14 |
import android.widget.Toast; |
15 |
|
16 |
public class MainActivity extends Activity { |
17 |
|
18 |
protected static final int RESULT_SPEECH = 1; |
19 |
|
20 |
private ImageButton btnSpeak; |
21 |
private TextView txtText; |
22 |
|
23 |
@Override |
24 |
public void onCreate(Bundle savedInstanceState) { |
25 |
super.onCreate(savedInstanceState); |
26 |
setContentView(R.layout.activity_main); |
27 |
|
28 |
txtText = (TextView) findViewById(R.id.txtText); |
29 |
|
30 |
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); |
31 |
|
32 |
btnSpeak.setOnClickListener(new View.OnClickListener() { |
33 |
|
34 |
@Override |
35 |
public void onClick(View v) { |
36 |
|
37 |
Intent intent = new Intent( |
38 |
RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
39 |
|
40 |
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); |
41 |
|
42 |
try { |
43 |
startActivityForResult(intent, RESULT_SPEECH); |
44 |
txtText.setText(""); |
45 |
} catch (ActivityNotFoundException a) { |
46 |
Toast t = Toast.makeText(getApplicationContext(), |
47 |
"Opps! Your device doesn't support Speech to Text", |
48 |
Toast.LENGTH_SHORT); |
49 |
t.show(); |
50 |
} |
51 |
} |
52 |
}); |
53 |
|
54 |
} |
55 |
|
56 |
@Override |
57 |
public boolean onCreateOptionsMenu(Menu menu) { |
58 |
getMenuInflater().inflate(R.menu.activity_main, menu); |
59 |
return true; |
60 |
} |
61 |
|
62 |
@Override |
63 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
64 |
super.onActivityResult(requestCode, resultCode, data); |
65 |
|
66 |
switch (requestCode) { |
67 |
case RESULT_SPEECH: { |
68 |
if (resultCode == RESULT_OK && null != data) { |
69 |
|
70 |
ArrayList<String> text = data |
71 |
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); |
72 |
|
73 |
txtText.setText(text.get(0)); |
74 |
} |
75 |
break; |
76 |
} |
77 |
|
78 |
} |
79 |
} |
80 |
} |
Android Speech to text Android API的核心是包
android.speech和類
android.speech.RecognizerIntent。我們觸發一個意圖(android.speech.RecognizerIntent)顯示對話框來識別語音輸入,這個Activity轉換語音為文本並把結果傳回我們正在調用的Activity。當我們調用android.speech.RecognizerIntent意圖時,必須使用
startActivityForResult()來接聽文本結果。
注意在上面的代碼中我們是怎樣創建並觸發意圖intent android.speech.RecognizerIntent的,同時使用.putExtra()方法添加了一個參數。調用RecognizerIntent時,必須提供
RecognizerIntent.EXTRA_LANGUAGE_MODE,在這里我們設置為
en-US。
由於我們的RecognizerIntent通過startActivityForResult()觸發,我們重寫了
onActivityResult(int requestCode, int resultCode, Intent data)方法來處理結果數據。RecognizerIntent會把語音轉換為文本並把結果通過鍵RecognizerIntent.EXTRA_RESULTS作為ArrayList傳回來。只有RESULT_OK返回時才會出現。我們只需要使用
txtText.setText()把從結果中拿到的文本設置到text view
texText中。
在這里值得注意的一件事是在不支持speech to text API的設備/Android版本中應該怎樣處理。在這種情況下,當我們視圖啟動Activity時ActivityNotFoundException異常會被拋出。在上面的例子中,我們捕獲了這個異常並使用Toast顯示了一個提示信息“Opps! Your device doesn’t support Speech to Text”。
Android應用程序的屏幕截圖
到這里就結束了! 在Android模擬器或真實設備上執行應用程序,將會看到下面的輸出。




