Android:使用Speech To Text API进行语音到文本转换


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="<A class=referer href="l#ACTION_RECOGNIZE_SPEECH Documentation

OSHINA原创翻译 ,  原文链接

相关内容