You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 9+CommunityToolkit.Maui 11 SpeechToText无法返回土耳其语结果

解决CommunityToolkit.Maui SpeechToText始终识别为英语的问题

一、检查平台端语音支持配置

Android

  • 确认设备已安装土耳其语(tr-TR)语音识别包:进入系统设置 → 语言和输入法 → 语音输入 → 安装语言包,找到土耳其语完成安装。
  • 确保应用已申请录音权限,在AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.RECORD_AUDIO" />

iOS

  • 检查设备系统语言支持土耳其语,且已开启应用的语音识别权限:进入设置 → 隐私与安全性 → 语音识别 → 允许目标应用访问。
  • 在Info.plist中添加语音识别权限描述:
<key>NSSpeechRecognitionUsageDescription</key>
<string>需要访问语音识别功能将语音转换为文本</string>

Windows

  • 确认系统已安装土耳其语语音包:进入设置 → 时间和语言 → 语言和区域 → 添加语言 → 选择土耳其语并安装相关语音组件。

二、修正代码逻辑问题

当前代码存在两个核心问题:事件绑定时机滞后,且返回值会因异步回调未触发提前返回空值。修正后的代码如下:

public async Task<string> MediaToolKitSpeechToTextAsync()
{
    var tcs = new TaskCompletionSource<string>();
    var options = new SpeechToTextOptions
    {
        Culture = CultureInfo.GetCultureInfo("tr-TR")
    };

    // 先绑定事件再启动监听,避免错过回调
    void OnRecognitionResultCompleted(object? s, SpeechToTextRecognitionResultCompletedEventArgs args)
    {
        // 解绑事件防止重复触发
        SpeechToText.Default.RecognitionResultCompleted -= OnRecognitionResultCompleted;
        
        if (args.RecognitionResult != null && !string.IsNullOrEmpty(args.RecognitionResult.Text))
        {
            tcs.TrySetResult(args.RecognitionResult.Text);
        }
        else
        {
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                await DisplayAlert("Hata", "Ses anlaşılamadı.", "Tamam");
                tcs.TrySetResult(string.Empty);
            });
        }
    }

    SpeechToText.Default.RecognitionResultCompleted += OnRecognitionResultCompleted;

    try
    {
        await SpeechToText.Default.StartListenAsync(options, CancellationToken.None);
    }
    catch (Exception ex)
    {
        SpeechToText.Default.RecognitionResultCompleted -= OnRecognitionResultCompleted;
        tcs.TrySetException(ex);
    }

    return await tcs.Task;
}

三、验证文化设置有效性

启动监听前可打印文化信息,确认是否正确设置为土耳其语:

Debug.WriteLine($"当前语音识别文化:{options.Culture.Name}");

若输出不是tr-TR,检查是否引用了System.Globalization命名空间,或尝试直接用字符串初始化:

Culture = new CultureInfo("tr-TR")

内容的提问来源于stack exchange,提问作者Hakan DOĞAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:01:06