语音转文字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模拟器或真实设备上执行应用程序,将会看到下面的输出。