音频输入和输出(Android代码)

时间:2012-01-15 19:26:45

标签: android speech-recognition voice-recognition

我如何在Android代码中更改音频输入和输出语言?我应该编辑此代码来更改音频输入输出语言吗? {我想将一个变量设置为En到语言英语或将变量设置为Tr以选择土耳其语,如果可能的话?}

 public Intent getRecognizeIntent()
    { 
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
        return intent;
    }

或者这有助于我改变? - > EXTRA_LANGUAGE_PREFERENCE

3 个答案:

答案 0 :(得分:1)

我将RecognizerIntent语言设置为土耳其语:

language = "tr-TR";
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, language);
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,language);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, language);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS, language);
startActivityForResult(intent, REQUEST_CODE);

答案 1 :(得分:0)

这在大多数情况下都适用:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault());

在哪里选择所需的语言环境并将其传递给toString()到EXTRA_LANGUAGE_PREFERENCE

但是,要正确,您应该尝试进行语言检查,并观察返回的“声音”。手动或自动选择所需的“语音”字符串,然后将其用于RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE值。

以下是执行语言检查的一些代码:

public static void getLanguageDetails(Context context,
        OnLanguageDetailsListener andThen)
{
    Intent detailsIntent = new Intent(
            RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    LanguageDetailsChecker checker = new LanguageDetailsChecker(andThen);
    context.sendOrderedBroadcast(detailsIntent, null, checker, null,
            Activity.RESULT_OK, null, null);
}

其中LanguageDetailsChecker是这样的:

public class LanguageDetailsChecker extends BroadcastReceiver
{
    private List<String> supportedLanguages;

    private String languagePreference;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
        {
            languagePreference =
                    results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
        }
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =
                    results.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
        }
        }
}

答案 2 :(得分:0)

如果要指定语言识别,则需要输入:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);

其中language是具有Locale格式的String。