Android的特定词的语音识别 [英] Android specific words speech recognition

查看:429
本文介绍了Android的特定词的语音识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有应用程序识别使用下面的code用户说某些词,但由于某些原因,它是不工作的。请仔细阅读这一点,并告诉我什么是错的。谢谢

该应用程序只是想显示敬酒消息,如果说的话橙色或苹果,但使用低于code当什么也没发生。

//按钮的onclick触发RecognizerIntent

 公共无效OnClick_Speed​​_Detector(视图V)
{
    意图I =新意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT,说话);
    startActivityForResult(I,1);}保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
    如果(要求code == 1安培;&安培;结果code == RESULT_OK)
    {
        ArrayList的<串GT;结果=
                data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);                如果(((结果).equals(橙色)))
                {
                  Toast.makeText(getApplicationContext(),橙色,Toast.LENGTH_LONG).show();                }
                其他
                    如果(((结果).equals(苹果)))
                {
                  Toast.makeText(getApplicationContext(),苹果,Toast.LENGTH_LONG).show();
                }    }
}


解决方案

您的问题是,如果一个ArrayList ==一个字符串,当它不可能(String类型的ArrayList包含多个字符串)

而不是:

  IF((结果).equals(橙色)){}

尝试:

  IF((结果)。载有(橙色)){}

这code会去翻ArrayList中的各项指标,并确定是否它等于橙色的指标。如果有任何做那么它将返回

 

...,它会执行if语句!
希望这有助于!

I am trying to have the app recognize certain words said by the user using the code below, but for some reason it isn't working at all. Please review this and tell me what is wrong with it. Thank you

The app is simply suppose to display a toast message if the words "orange" or "apple" is said but nothing happens when using the code below.

//button onclick to trigger RecognizerIntent

public void OnClick_Speed_Detector(View v)
{
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak up");
    startActivityForResult(i, 1);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == 1 && resultCode == RESULT_OK)
    {
        ArrayList<String> result = 
                data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if(((result).equals("orange")))
                {
                  Toast.makeText(getApplicationContext(), "orange", Toast.LENGTH_LONG).show();

                }   
                else
                    if (((result).equals("apple")))
                { 
                  Toast.makeText(getApplicationContext(), "apple", Toast.LENGTH_LONG).show();   
                }

    }
}

解决方案

Your issue is that you are testing if an ArrayList == a String, when it can't possibly (an ArrayList of type String contains multiple strings)

Instead of:

if ((result).equals("orange")) {}

Try:

if ((result).contains("orange")) {}

This code will look through every index of the ArrayList and determine if any of the indexes of it equal "orange". If any do then it will return

true

...and it will execute the if statement! Hope this helps!

这篇关于Android的特定词的语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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