C# 在我的文本中添加语音
我正在尝试将语音添加到我的计算器项目中。我能够添加声音。但我面临一些问题。当我按13键时,它先说“一”,然后说“十三”。还有一件事我不能给等号加上声音 这里是方法C# 在我的文本中添加语音,c#,C#,我正在尝试将语音添加到我的计算器项目中。我能够添加声音。但我面临一些问题。当我按13键时,它先说“一”,然后说“十三”。还有一件事我不能给等号加上声音 这里是方法 private void Equal(object sender, EventArgs e) { if (buffer.Length != 0) operand[1] = Double.Parse(buffer); switch (op) { case '+': re
private void Equal(object sender, EventArgs e)
{
if (buffer.Length != 0)
operand[1] = Double.Parse(buffer);
switch (op)
{
case '+': result = operand[0] + operand[1]; break;
case '-': result = operand[0] - operand[1]; break;
case '*': result = operand[0] * operand[1]; break;
case '/': result = operand[0] / operand[1]; break;
}
txtOutput.Text = result.ToString();
if (txtOutput.Text != "")
{
SpVoice voice = new SpVoice();
voice.Volume = 100;
// voice.Speak("The Result Is"+ SpeechVoiceSpeakFlags.SVSFlagsAsync);
voice.Speak(txtOutput.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
step = 1;
buffer = "";
}
这里是相等的方法。您必须将数字转换为单词,否则它会将13表示为1-3。一篇向你展示如何做到这一点的帖子 此外,对于equal,您还需要手动将其添加到您想要发言的文本中 基本上,您需要像这样构建文本,其中operationText将是“加”或“减”,等等: 我快速地看了一眼,看不到任何关于说数字的东西 正如我在评论中读到的那样,您可以调整代码以用文字表达数字 快速更改代码 在Visual studio中安装Humanizer nuget 然后将代码更改为
// using Humanizer;
voice.Speak(result.ToWords(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
ToWords()是一种扩展方法,它可以将您的数字转换为等效的单词,例如13到“十三”我可以使用任务和取消标记来解决它,代码如下: 我向类中添加了一个列表:
List<CancellationTokenSource> toCancel = new List<CancellationTokenSource>();
我不理解你的“=”问题,但是你可以用延迟来解决关于数字的问题。不要在每次打字后“说话”。启动计时器,如果500毫秒(或1秒)后没有任何变化,则讲话。在每个字符后重置计时器。只有当用户暂停时,语音才会启动(当然你不能确定他何时完成键入,但暂停将处理大多数常见情况)。他说,你需要一个软件包,这样可以将数字和符号转换为英语单词。
// using Humanizer;
voice.Speak(result.ToWords(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
List<CancellationTokenSource> toCancel = new List<CancellationTokenSource>();
private void NumberButtons(object sender, EventArgs e)
{
Button b = sender as Button;
if ((b == null) || (b.Text == "0" && buffer.Length == 0))
return;
buffer += b.Text;
txtOutput.Text = buffer;
if (txtOutput.Text != "")
{
if (toCancel.Count > 0)
{
foreach (var tc in toCancel)
{
tc.Cancel();
}
}
CancellationTokenSource ts = new CancellationTokenSource();
CancellationToken ct;
ct = ts.Token;
toCancel.Add(ts);
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
if (ct.IsCancellationRequested == false)
{
SpVoice voice = new SpVoice();
voice.Volume = 100;
voice.Speak(txtOutput.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
}, ct);
}
}