zoukankan      html  css  js  c++  java
  • 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);
    }
    }
  • 相关阅读:
    表格标签
    图片标签
    超链接标签
    媒体标签
    实体标签
    html常用的标签
    头信息的作用
    【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序
    【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛
    【bzoj4010】[HNOI2015]菜肴制作 拓扑排序+堆
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2592954.html
Copyright © 2011-2022 走看看