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

发布于 2024-12-27 06:42:25 字数 590 浏览 4 评论 0原文

如何更改我的 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

How i change audio input and output language in my android code? Should i edit this code to change audio input output language ? {I want to set one variable to En to speech language English or set variable to Tr to select Turkish if this possible?}

 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;
    }

Or this help me to change? -> EXTRA_LANGUAGE_PREFERENCE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞花火° 2025-01-03 06:42:25

我将 RecognizerIntent 语言设置为土耳其语,如下所示:

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

I set RecognizerIntent language to Turkish like that:

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);
浪漫之都 2025-01-03 06:42:25

这应该在大多数情况下有效:

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

或者

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

在您选择所需的区域设置并将其传递给 EXTRA_LANGUAGE_PREFERENCE 的 toString() 的地方

但是,为了正确起见,您应该尝试进行语言检查,并观察返回的“声音”。手动或自动选择所需的“语音”字符串,然后将其用作 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);
        }
        }
}

This should work in most cases:

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

or

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

Where you pick the locale you want and pass it's toString() to EXTRA_LANGUAGE_PREFERENCE

However, to be correct, you should try doing a language check, and observing the returned "voices". Manually or automatically, pick the "voice" string you want and then use that for the RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE value.

Here is some code to execute the language check:

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);
}

where LanguageDetailsChecker is something like this:

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);
        }
        }
}
梦里人 2025-01-03 06:42:25

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

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);

其中 language 是具有区域设置格式的字符串。

If you want to specify a language recognition you need to put:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);

where language is a String with the Locale format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文