• Android语音识别功能使用


    package com.king.android.speical;
    import java.util.ArrayList;
    import com.king.android.R;
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.os.Bundle;
    import android.speech.RecognizerIntent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    /**
     * 描述:语音识别功能(只有在真机才能测试)
     * 作者:Andy.Liu
     * 时间: 2012-7-16  上午07:38:27
     **/
    public class VoiceActivity extends Activity implements OnClickListener{
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnVoice = (Button) findViewById(R.id.btn_voice);
    btnVoice.setVisibility(View.VISIBLE);
    btnVoice.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    switch(v.getId()){
    case R.id.btn_voice:
    doStartVoice();
    }
    }
    /**
    *TODO:开始语音
    *Author:Andy.Liu
    *Create Time:2012-7-16 上午07:42:48
    *TAG:
    *Return:void
    */
    private void doStartVoice(){
    try{
    //通过intent传递语音识别的模式,
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //语言模式和自由形式的主意识别
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //提示语音开始
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }catch(ActivityNotFoundException e){
    //找到到语音设备
    Toast.makeText(VoiceActivity.this, "没有找到设备", Toast.LENGTH_LONG).show();
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(VOICE_RECOGNITION_REQUEST_CODE==requestCode&&RESULT_OK ==resultCode){
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    StringBuilder sb = new StringBuilder();
    for(String voice:results){
    sb.append(voice);
    }
    Toast.makeText(VoiceActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
    }
    }
  • 相关阅读:
    c#数据绑定(3)——数据转化为信息
    c#数据绑定(2)——删除DataTable的数据
    C # 数据绑定(1)——将DataTabel的data添加ListView
    如何下载Chrome离线版EXE安装文件和MSI版安装文件
    Windows Installer (MSI) 详解 参数介绍
    7za.exe 命令行用法,参数介绍
    命令行启动Win7系统操作部分功能
    升级WordPress后开启友情链接管理模块
    如何将文件所有者改为TrustedInstaller
    开机自检时出现问题后会出现各种各样的英文短句解说
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2592954.html
Copyright © 2020-2023  润新知