语音

来源:互联网 发布:结构主义知乎 编辑:程序博客网 时间:2024/06/11 10:10

Google的语音识别是有目共睹的,所以Android上面也是沾了大光了,用起来简单至极。

过程如下:

1、启动语音识别Activity

2、这里处理语音(传到google服务器处理)

3、结果以Acitivity的结果返回(onActivityResult)

主要用到的类为android.speech.RecognizerIntent

下面的例子参考了API Demo。

[java] view plaincopy
  1. package com.linc;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.content.pm.PackageManager;  
  9. import android.content.pm.ResolveInfo;  
  10. import android.os.Bundle;  
  11. import android.speech.RecognizerIntent;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.TextView;  
  16.   
  17. public class VoiceRecognitionDemoActivity extends Activity {  
  18.     private static final String TAG = "VoiceRecognition";  
  19.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  20.       
  21.     private TextView textView;  
  22.     private Button button;  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         initWidget();  
  29.           
  30.         // Check to see if a recognition activity is present  
  31.         PackageManager pm = getPackageManager();  
  32.         List<ResolveInfo> activities = pm.queryIntentActivities(  
  33.                 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  34.         if (activities.size() != 0) {  
  35.             button.setOnClickListener(new OnClickListener() {  
  36.                 @Override  
  37.                 public void onClick(View v) {  
  38.                     startVoiceRecognitionActivity();  
  39.                 }  
  40.             });  
  41.         } else {  
  42.             button.setEnabled(false);  
  43.             button.setText("Recognizer not present");  
  44.         }  
  45.     }  
  46.       
  47.     private void initWidget()  
  48.     {  
  49.         textView = (TextView)findViewById(R.id.tv);  
  50.         button = (Button)findViewById(R.id.btn);  
  51.     }  
  52.       
  53.     /** 
  54.      * Fire an intent to start the speech recognition activity. 
  55.      */  
  56.     private void startVoiceRecognitionActivity() {  
  57.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  58.   
  59.         // Display an hint to the user about what he should say.  
  60.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "请说标准普通话");//注意不要硬编码  
  61.   
  62.         // Given an hint to the recognizer about what the user is going to say  
  63.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  64.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  65.   
  66.         // Specify how many results you want to receive. The results will be sorted  
  67.         // where the first result is the one with higher confidence.  
  68.         intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);//通常情况下,第一个结果是最准确的。  
  69.   
  70.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  71.     }  
  72.       
  73.     @Override  
  74.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  75.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {  
  76.             // Fill the list view with the strings the recognizer thought it could have heard  
  77.             ArrayList<String> matches = data.getStringArrayListExtra(  
  78.                     RecognizerIntent.EXTRA_RESULTS);  
  79.             StringBuilder stringBuilder = new StringBuilder();  
  80.             int Size = matches.size();   
  81.             for(int i=0;i<Size;++i)  
  82.             {  
  83.                 stringBuilder.append(matches.get(i));  
  84.                 stringBuilder.append("\n");  
  85.             }  
  86.             textView.setText(stringBuilder);  
  87.         }  
  88.   
  89.         super.onActivityResult(requestCode, resultCode, data);  
  90.     }  
  91. }  
结论:

在wifi(家里的,1兆带宽)状态下,识别速度飞快。

语音包是买手机时预装好的。

识别率很高。中文、英文、数字都能很好的识别。

用手机的gprs,效果不尽如人意。几次识别都用了30秒左右的时间,体验很糟。


原创粉丝点击