如何根据语音识别结果使用拨动开关 [英] how to use toggle switch by result of speech recognition

查看:44
本文介绍了如何根据语音识别结果使用拨动开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过诸如打开和关闭之类的语音命令使用切换开关,所以我从网站获得了一个用于语音识别的代码,但不知道如何通过它触发我的切换按钮

i want use toggle switch by voice commands like switch on and switch off so i got a code for voice recognition from a site but dont know how to trigger my toggle button thru it

我使用的语音识别代码-

The code of voice recognition i used -

package com.authorwjf.talk2me;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

protected static final int REQUEST_OK = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button1).setOnClickListener(this);
}

@Override
public void onClick(View v) {
     Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
     try {
         startActivityForResult(i, REQUEST_OK);
     } catch (Exception e) {
         Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
     }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
        ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        ((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));



    }
}

}

推荐答案

你的数组 thingsYouSaid 拥有所有可能的字符串数组.例如,如果我说 hello 它会像 [hello,aloe,hallo,no] 所以你要做的是你可以匹配你的字符串 关闭 到结果字符串数组,如果它与 "switch off" 匹配,那么同样将开关的值从打开更改为关闭;

your array thingsYouSaid have all possible string array you have . for example if I say hello it will have like [hello,aloe,hallo,no] so what you have to do is you can match your string switch off to result string array and if it is match with like "switch off" than change value of your switch from on to off likewise;

    if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
            ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

    for(String value : thingsYouSaid.get(0)){

    if(value.equalsignorecase("switch off")){
      // change value for switch to off

       break;
    }
    else if(value.equalsignorecase("switch on")){
    // change value for switch to on
    break;
    }


 }

这篇关于如何根据语音识别结果使用拨动开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆