问题描述
我想创建一个简单的检测,用户可以对着设备和活动将取决于什么用户发言做一些特定的短语的应用程序。我的日子不好过这个具体的事情找教程,请帮助我。到目前为止,我已经创建了一个按钮,将启动识别意图,我有一个的onActivityResult,我希望能够检测用户什么是说,然后调用根据用户所说的短语的特定功能。
公共无效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)
{
//如果用户说的那句poptarts
//调用函数poptart //如果用户说的那句香蕉
//调用函数香蕉 Toast.makeText(getApplicationContext(),发声,Toast.LENGTH_LONG).show();
}
}
有一个看这个,这是你的code应该如何工作的:
公共无效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){
//如果用户说的那句poptarts
//调用函数poptart //如果用户说的那句香蕉
//调用函数香蕉
ArrayList的<字符串>结果= data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); ArrayList的<字符串>结果= data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
如果(((结果).equals(T1))){
意向意图=新意图(此,* * * ClassToOpen的* .class);
startActivity(意向);
}否则如果(((结果).equals(T2))){
意向意图=新意图(此,* * * ClassToOpen的* .class);
startActivity(意向);
} Toast.makeText(getApplicationContext(),发声,Toast.LENGTH_LONG).show();
}
super.onActivityResult(要求code,结果code,数据);
}
把所需的字符串为 T1 和 T2
希望这有助于投票或接受,如果它没有。
I am trying to create an app that simply detects specific phrases that the user can speak into the device and the activity will do something depending on what the user has spoken. I had a hard time finding tutorials on this specific thing so please help me out. So far I have created a button that will start the Recognizer Intent and I have a onActivityResult which I hope can detect what the user is saying and then call specific functions depending on the phrase the user has spoken.
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)
{
//if user says the phrase "poptarts"
//call function poptart
//if user says the phrase "banana"
//call function banana
Toast.makeText(getApplicationContext(), "sound detected", Toast.LENGTH_LONG).show();
}
}
Have a look at this, it's how your code should be to work:
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) {
//if user says the phrase "poptarts"
//call function poptart
//if user says the phrase "banana"
//call function banana
ArrayList < String > result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayList < String > result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (((result).equals(t1))) {
Intent intent = new Intent(this, * * ClassToOpen * * .class);
startActivity(intent);
} else if (((result).equals(t2))) {
Intent intent = new Intent(this, * * ClassToOpen * * .class);
startActivity(intent);
}
Toast.makeText(getApplicationContext(), "sound detected", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
Put the desired string as t1 and t2.
Hope this helps, vote up or accept if it did.
这篇关于Android的语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!