speech to text

Android自带的内置功能演讲文字,您可以通过它提供语音输入到您的应用程序。通过这种方式,您可以添加一些很酷的功能到您的应用程序,如添加语音导航(有用的,当您针对残疾人),填写表单与语音输入等,

在语音输入工作的背景下,语音输入将被流式传输到服务器,服务器上的语音将被转换为文本,最后文本将被发送回我们的应用程序。

如果你想做另一种方式,即将文本转换为语言,请按照我之前的教程Android文字转语音

下载代码
我已经创建了一个简单的应用程序来演示本教程。以下是应用程序的屏幕截图,其中包含一个简单的按钮来调用语音输入和一个TextView来显示已转换的语音文本。

android语音到文本
所以让我们从创建简单的应用程序开始。

样品申请
1。通过转到文件⇒新建⇒Android应用程序项目并提供所需的信息,在Eclipse中创建一个新的项目。

2。打开位于res⇒值之下的strings.xml,并添加以下字符串值。

strings.xml中

<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
     < string name = "app_name" >Speech To Text</ string >
     < string name = "action_settings" >Settings</ string >
     < string name = "hello_world" >Hello world!</ string >
     < string name = "speech_prompt" >Say something…</ string >
     < string name = "speech_not_supported" >Sorry! Your device doesn\'t support speech input</ string >
     < string name = "tap_on_mic" >Tap on mic to speak</ string >
</ resources >

3。打开colors.xml位于res⇒ 值下方,并添加以下颜色。如果您没有看到colors.xml,请创建一个新文件并添加值。

colors.xml

<? xml version = "1.0" encoding = "utf-8" ?>
	< resources >
	     < color name = "white" >#ffffff</ color >
	     < color name = "bg_gradient_start" >#31244e</ color >
	     < color name = "bg_gradient_end" >#6b394c</ color >
	</ resources >

4。现在打开主要活动(activity_main.xml)的布局文件,并添加以下代码来创建一个简单的布局。

activity_main.xml中
 < RelativeLayout xmlns:android = “http://schemas.android.com/apk/res/android”
 xmlns:tools = “http://schemas.android.com/tools”
 android:layout_width = “match_parent”
 android:layout_height = “match_parent”
 android:background = “@drawable/bg_gradient”
 android:orientation = “vertical” >
< TextView
         android:id = "@+id/txtSpeechInput"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignParentTop = "true"
         android:layout_centerHorizontal = "true"
         android:layout_marginTop = "100dp"
         android:textColor = "@color/white"
         android:textSize = "26dp"
         android:textStyle = "normal" />
 
     < LinearLayout
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignParentBottom = "true"
         android:layout_centerHorizontal = "true"
         android:layout_marginBottom = "60dp"
         android:gravity = "center"
         android:orientation = "vertical" >
 
         < ImageButton
             android:id = "@+id/btnSpeak"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:background = "@null"
             android:src = "@drawable/ico_mic" />
 
         < TextView
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_marginTop = "10dp"
             android:text = "@string/tap_on_mic"
             android:textColor = "@color/white"
             android:textSize = "15dp"
             android:textStyle = "normal" />
     </ LinearLayout >
 
</ RelativeLayout >

5。最后打开您的MainActivity.java并进行以下更改。在简单的添加语音输入将分两步完成。

第1步:启动RecognizerIntent
首先,我们需要设置必要的标志,如创建RecognizerIntent
ACTION_RECOGNIZE_SPEECH -只需花费用户的语音输入,并将其返回到相同的活动
LANGUAGE_MODEL_FREE_FORM -虑输入以游离形式英文
EXTRA_PROMPT -文字提示,展现给用户,询问时他们说话

步骤2:接收语音响应
一旦语音输入完成,我们必须在onActivityResult中捕获响应,并采取适当的措施。

MainActivity.java
package info.androidhive.speechtotext;
 
import java.util.ArrayList;
import java.util.Locale;
 
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 {

 private TextView txtSpeechInput;
 private ImageButton btnSpeak;
 private final int REQ_CODE_SPEECH_INPUT = 100 ;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
     btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

     // hide the action bar
     getActionBar().hide();

     btnSpeak.setOnClickListener( new View.OnClickListener() {

         @Override
         public void onClick(View v) {
             promptSpeechInput();
         }
     });

 }

 /**
  * Showing google speech input dialog
  * */
 private void promptSpeechInput() {
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
             RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
             getString(R.string.speech_prompt));
     try {
         startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
     } catch (ActivityNotFoundException a) {
         Toast.makeText(getApplicationContext(),
                 getString(R.string.speech_not_supported),
                 Toast.LENGTH_SHORT).show();
     }
 }

 /**
  * Receiving speech input
  * */
 @Override
 protected void onActivityResult( int requestCode, int resultCode, Intent data) {
     super .onActivityResult(requestCode, resultCode, data);

     switch (requestCode) {
     case REQ_CODE_SPEECH_INPUT: {
         if (resultCode == RESULT_OK && null != data) {

             ArrayList<String> result = data
                     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
             txtSpeechInput.setText(result.get( 0 ));
         }
         break ;
     }

     }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main, menu);
     return true ;
 }

}
在一个真实的设备上运行应用程序。确保设备在测试时具有良好的互联网连接。