如何在Azure函数中使用'Microsoft.CognitiveServices.Speech'命名空间?

时间:2019-07-11 20:02:18

标签: c# azure azure-functions text-to-speech

我正在测试Microsoft Azure语音服务,特别是尝试使用“文本到语音”功能。因此,我使用了免费的Azure层,并创建了一个TimeTrigger Azure函数来读取电子邮件,遍历HTML,然后使用SDK Microsoft.CognitiveServices.Speech调用语音服务。我正在使用function.proj加载nuget程序包,同时加载S22.ImapHtmlAgilityPack没有任何问题。但是语音包触发了一个异常: Unable to load DLL 'Microsoft.CognitiveServices.Speech.core.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)。 我可以在Azure功能中使用此程序包吗?如果是这样,我在做什么错了?

我试图从<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />中删除function.proj行,并删除了project.assets.json以重新加载程序包,但是它不起作用。

这是我的function.proj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="S22.Imap" Version="3.6.0" />
        <PackageReference Include="HtmlAgilityPack" Version="1.11.9" />
        <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />
    </ItemGroup>
</Project>

这是我的run.csx

using System;
using S22.Imap;
using System.Net.Mail;
using HtmlAgilityPack;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using System.Diagnostics;

public static void Run(TimerInfo myTimer, ILogger log)
{
    var username = "sample@gmail.com";
    var password = "sample";
    var subsKey = "sample";

    using(ImapClient client = new ImapClient("imap.gmail.com", 993, username, password, AuthMethod.Login, true))
    {
        IEnumerable<uint> uids = client.Search(SearchCondition.From("sample@sample.com"));
        IEnumerable<MailMessage> messages = client.GetMessages(uids);
        log.LogInformation($"Count: {messages.Count()}.");

        var msg = messages.FirstOrDefault();
        if(msg != null)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(msg.Body);

            var paragraphs = doc.DocumentNode.Descendants()
                .Where(x => x.Name == "p" && !string.IsNullOrEmpty(x.InnerText.Trim()))
                .ToList();

            var mailText = string.Empty;
            foreach(var par in paragraphs)
                mailText += par.InnerText;

            if(!string.IsNullOrEmpty(mailText))
            {
                var config = SpeechConfig.FromSubscription(subsKey, "myregion");
                config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);
                config.SpeechSynthesisLanguage = "pt-BR";

                using (var synthesizer = new SpeechSynthesizer(config))
                {
                    using (var result = synthesizer.SpeakTextAsync(mailText).Result)
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            //Do something with it
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            log.LogError($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                log.LogError($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                log.LogError($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试删除function.proj,然后重新创建一个并首先添加Microsoft.CognitiveServices.Speech

确保Microsoft.CognitiveServices.Speech.core.dllwin-x86中已安装win-x64。请参阅此issue

enter image description here