c# - 取消选择项目时如何停止语音 Windows.Media.SpeechSynthesis
问题描述
我目前正在尝试添加功能以允许在我的应用程序中使用屏幕阅读器,并且一直在使用 Windows.Media.SpeechSynthesis 和 .GotFocus 在屏幕阅读设备选择按钮和标签时读出它们。
My issue however is that the speech doesn't stop when another item is selected so I have everything being read at once.
我曾尝试使用 .LostFocus 和 .Stop() 方法来尝试解决此问题,但遗憾的是无济于事。
有没有人幸运地在应用程序和项目选择中实现 SpeechSynthesis?
解决方案
这是我在我的一个应用程序中使用的一些相关代码,
我使用单个实例MediaElement
来阅读文本。
static MediaElement ttsMediaElement;
这是将文本转换为流并读取它的方法。在方法开始时,我停止了MediaElement
.
public static async Task ConvertTextToSpeechAndPlay(string text)
{
if (ttsMediaElement != null)
ttsMediaElement.Stop(); //stop the reading that has not finished, if any
var voice = ChooseVoice(text); //select the preferred voice chosen by the user
if (voice != null)
{
using (var synth = new SpeechSynthesizer())
{
synth.Voice = voice;
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text);
ttsMediaElement = new MediaElement();
ttsMediaElement.SetSource(stream, stream.ContentType);
ttsMediaElement.Play();
}
}
}
推荐阅读
- uwp - 有关媒体播放器为何会阻止 UI 输入约 30 秒的任何信息
- excel - 尝试通过 VLookup 将单元格从一张表复制到另一张表
- javascript - 两个日期之间明显的 1-Step-Difference
- r - R中的lm(公式)在parLapply中表现不同
- sas - 如何使用通过在宏中使用 SYSPBUFF 传递的变量列表来屏蔽子宏中的“或”
- windows - 为什么WOW64 PEB中模块的文件路径指向x64版本的模块?
- ms-access - 如何构建具有多个选项的物料清单
- sql - 在 SQL 查询之后,需要帮助从 ForEach 返回记录
- qbfc - QBFC - 错误模块名称:ucrtbase.DLL 异常代码:0xc0000409
- c - 如果变量在声明时初始化并链接到 NOLOAD 部分,有没有办法引发链接时间错误?