六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 629|回复: 0

android语音识别示例

[复制链接]
 楼主| 发表于 2013-2-5 01:57:28 | 显示全部楼层 |阅读模式
  1. package com.angle.anglering;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;

  5. import java.util.ArrayList;
  6. import android.content.ActivityNotFoundException;
  7. import android.content.Intent;
  8. import android.speech.RecognizerIntent;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.Toast;

  13. public class AngleRingActivity extends Activity {

  14.          private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
  15.          private Button btn_voice = null;
  16.          
  17.         @Override
  18.         protected void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.activity_angle_ring);
  21.                

  22.                
  23.                 /** * 语音识别示例 . 注意:使用前需要安装语音识别程序如语音搜索。 * /
  24.                  /** Called when the activity is first created. */
  25.                 btn_voice = (Button) findViewById(R.id.btn_voice);
  26.                 btn_voice.setOnClickListener(new OnClickListener() {
  27.                         @Override
  28.                         public void onClick(View arg0) {
  29.                                 try {
  30.                                         // 通过Intent传递语音识别的模式,开启语音
  31.                                         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  32.                                         // 语音模式和自由形式的语音识别
  33.                                         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  34.                                         // 提示语音开始
  35.                                         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");
  36.                                         // 开始执行我们的intent、语音识别
  37.                                         startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
  38.                                   } catch (ActivityNotFoundException e) {
  39.                                           // 找不到语音设备装置
  40.                                           System.out.println("找不到语音设备装置!");
  41.                                           e.printStackTrace();
  42.                                           Toast.makeText(AngleRingActivity.this,"ActivityNotFoundException", Toast.LENGTH_LONG).show();
  43.                                           }
  44.                         }});
  45.                                
  46.         }

  47.         /** * 当语音结束时的回调函数onActivityResult */
  48.         @Override
  49.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  50.                 // 判断是否是我们执行的语音识别
  51.                 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
  52.                         // 取得语音的字符
  53.                         ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  54.                         String resultsString = "";
  55.                         for (int i = 0; i < results.size(); i++) {
  56.                                 resultsString += results.get(i);
  57.                         }
  58.                         Toast.makeText(this, resultsString, Toast.LENGTH_LONG).show();
  59.                         super.onActivityResult(requestCode, resultCode, data);
  60.                         }
  61.                 }
  62.        

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

  69. }
复制代码
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表