• 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);
    }
    }
  • 相关阅读:
    html5结构标签
    video标签、audio标签
    a标签、img图片、iframe、表单元素、div
    pre,html转义,abbr缩写,表格table
    html 列表标签
    p,br,hn,b,i,u,s,sup,sub标签
    网站开发准备学习
    交互原型设计工具
    为什么要重写hashCode()方法和equals()方法及如何重写
    Java接口回调
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2592954.html
Copyright © 2020-2023  润新知