語音轉文字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模擬器或真實設備上執行應用程序,將會看到下面的輸出。
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM