gpt4 book ai didi

java - Android 中的语音识别

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:53:51 25 4
gpt4 key购买 nike

我正在从事语音识别工作,需要一些示例程序。

谁能指导我?

最佳答案

让我剪切并粘贴一些内容,向您展示您需要的代码。

编辑:您还可以从 this project 下载方便的抽象类.

您将需要此 Intent (按您认为合适的方式进行参数化):

public Intent getRecognizeIntent(String promptToUse, int maxResultsToReturn)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResultsToReturn);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, promptToUse);
return intent;
}

然后您需要像这样将您的 Intent 发送到语音识别 Activity ,

public void gatherSpeech(String prompt)
{
Intent recognizeIntent = getRecognizeIntent(prompt);
try
{
startActivityForResult(recognizeIntent, SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE);
}
catch (ActivityNotFoundException actNotFound)
{
Log.w(D_LOG, "did not find the speech activity, not doing it");
}
}

然后您需要让您的 Activity 处理语音结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("Speech", "GOT SPEECH RESULT " + resultCode + " req: "
+ requestCode);
if (requestCode == SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Log.d(D_LOG, "matches: ");
for (String match : matches)
{
Log.d(D_LOG, match);
}
}
}
}

关于java - Android 中的语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3042752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com