Android 语音输入API使用

  1. Android 语音输入API使用 转载http://www.javaarch.net/jiagoushi/782.htm 
  2.   
  3. Android已经支持语音输入的API了,不过不知道中文输入识别效果怎么样。这里给一个怎么使用语音输入的示例  
  4.   
  5. 首先在android工程中的页面布局文件中res/layout/main.xml添加一个button和text  
  6.   
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.         xmlns:tools="http://schemas.android.com/tools"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_above="@+id/textView1"  
  12.         android:layout_toLeftOf="@+id/textView1"  
  13.         android:gravity="center"  
  14.         android:orientation="vertical" >  
  15.        
  16.         <ImageButton  
  17.             android:id="@+id/btnSpeak"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_margin="10dp"  
  21.             android:layout_marginRight="10dp"  
  22.             android:layout_marginTop="10dp"  
  23.             android:contentDescription="@string/speak"  
  24.             android:src="@android:drawable/ic_btn_speak_now" />  
  25.        
  26.         <TextView  
  27.             android:id="@+id/txtText"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_marginLeft="10dp"  
  31.             android:layout_marginRight="10dp"  
  32.             android:layout_marginTop="10dp"  
  33.             android:textAppearance="?android:attr/textAppearanceLarge" />  
  34.        
  35.     </LinearLayout>  
  36.       
  37. active类SpeechToTextDemoActivity.java  
  38.   
  39.     package net.viralpatel.android.speechtotextdemo;  
  40.        
  41.     import java.util.ArrayList;  
  42.        
  43.     import android.app.Activity;  
  44.     import android.content.ActivityNotFoundException;  
  45.     import android.content.Intent;  
  46.     import android.os.Bundle;  
  47.     import android.speech.RecognizerIntent;  
  48.     import android.view.Menu;  
  49.     import android.view.View;  
  50.     import android.widget.ImageButton;  
  51.     import android.widget.TextView;  
  52.     import android.widget.Toast;  
  53.        
  54.     public class MainActivity extends Activity {  
  55.        
  56.         protected static final int RESULT_SPEECH = 1;  
  57.        
  58.         private ImageButton btnSpeak;  
  59.         private TextView txtText;  
  60.        
  61.         @Override  
  62.         public void onCreate(Bundle savedInstanceState) {  
  63.             super.onCreate(savedInstanceState);  
  64.             setContentView(R.layout.activity_main);  
  65.        
  66.             txtText = (TextView) findViewById(R.id.txtText);  
  67.        
  68.             btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);  
  69.        
  70.             btnSpeak.setOnClickListener(new View.OnClickListener() {  
  71.        
  72.                 @Override  
  73.                 public void onClick(View v) {  
  74.        
  75.                     Intent intent = new Intent(  
  76.                             RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  77.        
  78.                     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");  
  79.        
  80.                     try {  
  81.                         startActivityForResult(intent, RESULT_SPEECH);  
  82.                         txtText.setText("");  
  83.                     } catch (ActivityNotFoundException a) {  
  84.                         Toast t = Toast.makeText(getApplicationContext(),  
  85.                                 "Opps! Your device doesn't support Speech to Text",  
  86.                                 Toast.LENGTH_SHORT);  
  87.                         t.show();  
  88.                     }  
  89.                 }  
  90.             });  
  91.        
  92.         }  
  93.        
  94.         @Override  
  95.         public boolean onCreateOptionsMenu(Menu menu) {  
  96.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  97.             return true;  
  98.         }  
  99.        
  100.         @Override  
  101.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  102.             super.onActivityResult(requestCode, resultCode, data);  
  103.        
  104.             switch (requestCode) {  
  105.             case RESULT_SPEECH: {  
  106.                 if (resultCode == RESULT_OK && null != data) {  
  107.        
  108.                     ArrayList<String> text = data  
  109.                             .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  110.        
  111.                     txtText.setText(text.get(0));  
  112.                 }  
  113.                 break;  
  114.             }  
  115.        
  116.             }  
  117.         }  
  118.     }  
  119.       
  120. 这里android.speech是Android语音输入的核心包,android.speech.RecognizerIntent是一个主要的类,这个active会弹出一个语音输入对话框,然后接收语音输入,识别语音内容转为文本,但我们启动语音输入active后,需要通过startActivityForResult()方法接收文本结果。在.putExtra()方法还需要输入RecognizerIntent.EXTRA_LANGUAGE_MODE语言类型,这里是en-US。  
  121.   
  122. 我们通过覆盖onActivityResult(int requestCode, int resultCode, Intent data)方法来处理结果数据,通过data获取key为RecognizerIntent.EXTRA_RESULTS来接收文本内容list,然后设置到text框上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值