我正在测试 Microsoft Azure 语音服务,特别是尝试使用文本转语音。因此,我使用 Azure 的免费层,并创建了一个 TimeTrigger Azure 函数来读取电子邮件、遍历 HTML,然后使用 SDK Microsoft.CognitiveServices.Speech
调用语音服务。 。我正在使用function.proj
加载 nuget 包,加载 S22.Imap
和HtmlAgilityPack
没有任何问题。但是语音包触发了异常:
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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a293b372a363f1a3d373b333674393537" rel="noreferrer noopener nofollow">[email protected]</a>";
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e192808c918d84a192808c918d84cf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>"));
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}]");
}
}
}
}
}
}
}
}
最佳答案
您可以尝试删除function.proj
,然后重新创建一个并首先添加Microsoft.CognitiveServices.Speech
。
确保 Microsoft.CognitiveServices.Speech.core.dll
已安装在 win-x86
和 win-x64
中。请引用这个issue .
关于c# - 如何将 'Microsoft.CognitiveServices.Speech' 命名空间与 Azure Functions 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996516/