如何在MAUI中实现语音转文本?

问题描述 投票:0回答:1

我想在 .Net MAUI 中将语音转换为文本。我搜索相同的内容但没有找到解决方案。

请让我知道如何实施。

我点击此链接:https://devblogs.microsoft.com/dotnet/speech-recognition-in-dotnet-maui-with-community-toolkit/

代码如下:

private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
    {
        CancellationToken cancellationToken = new CancellationToken();
        cancellationToken = CancellationToken.None;
        var isGranted = await SpeechToText.Default.RequestPermissions(cancellationToken);
        if (!isGranted)
        {
            await Toast.Make("Permission not granted").Show(CancellationToken.None);
            return;
        }
        var recognitionResult = await SpeechToText.Default.ListenAsync(
                                            CultureInfo.GetCultureInfo("uk-ua"),
                                            new Progress(partialText =>
                                            {
                                                RecognitionText += partialText + " ";
                                            }), cancellationToken);
        if (recognitionResult.IsSuccessful)
        {
           string RecognitionText = recognitionResult.Text;
        }
        else
        {
            await Toast.Make(recognitionResult.Exception?.Message ?? "Unable to recognize speech").Show(CancellationToken.None);
        }
    }

错误如下图所示

请分享您的建议,提前谢谢。

c# speech-recognition maui text-to-speech
1个回答
0
投票

您可以在微软文档中查看关于SpeechToText的代码。

这是在 C# 中使用的代码:

async Task Listen(CancellationToken cancellationToken)
{
    var isGranted = await speechToText.RequestPermissions(cancellationToken);
    if (!isGranted)
    {
        await Toast.Make("Permission not granted").Show(CancellationToken.None);
        return;
    }

    var recognitionResult = await speechToText.ListenAsync(
                                        CultureInfo.GetCultureInfo(Language),
                                        new Progress<string>(partialText =>
                                        {
                                            RecognitionText += partialText + " ";
                                        }), cancellationToken);

    if (recognitionResult.IsSuccessful)
    {
        RecognitionText = recognitionResult.Text;
    }
    else
    {
        await Toast.Make(recognitionResult.Exception?.Message ?? "Unable to recognize speech").Show(CancellationToken.None);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.