語音轉文字demo

Android有一個很是酷的特性不少開發者都還不知道。Any.DO之類應用的語音到文本轉換功能頗有創意。在如今Siri的世界裏,語音指令是極其重要的。Android原生提供Speech To Text功能,爲何不把它用在咱們的程序中!
 
我將會展現如何在程序中使用Android的Speech To Text API,如今開始寫咱們的demo程序。
 
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/textView1" android:layout_toLeftOf="@+id/textView1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/btnSpeak" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:contentDescription="@string/speak" android:src="@android :drawable/ic_btn_speak_now" /> <TextView android:id="@+id/txtText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>

 

 
第三步:觸發Speech to Text API的Android Java代碼
打開SpeechToTextDemoActivity 類並替換爲下面的代碼:
File: SpeechToTextDemoActivity.java
package net.viralpatel.android.speechtotextdemo;
 
import java.util.ArrayList; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.Menu; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { protected static final int RESULT_SPEECH = 1; private ImageButton btnSpeak; private TextView txtText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtText = (TextView) findViewById(R.id.txtText); btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); btnSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); try { startActivityForResult(intent, RESULT_SPEECH); txtText.setText(""); } catch (ActivityNotFoundException a) { Toast t = Toast.makeText(getApplicationContext(), "Opps! Your device doesn't support Speech to Text", Toast.LENGTH_SHORT); t.show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_SPEECH: { if (resultCode == RESULT_OK && null != data) { ArrayList<String> text = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); txtText.setText(text.get(0)); } break; } } } }

 

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模擬器或真實設備上執行應用程序,將會看到下面的輸出。
 
 
 
 
相關文章
相關標籤/搜索