【问题标题】:How to Stop SpeakAsync if another message were about to speak out?如果另一条消息要说出来,如何停止 SpeakAsync?
【发布时间】:2015-07-22 03:39:41
【问题描述】:

我正在尝试使用 c# 作为我的代码创建一个具有语音识别功能的网页。 请帮助我,因为我想知道如何在另一条消息开始讲话时停止 SpeakAsync 以传递完整的消息。

这样的伪代码

 protected void Button1_Click(object sender, EventArgs e)
    {
        ss.SpeakAsync(CurrentStop); // it means if there is a current message it should stop before delivering the next line
        ss.SpeakAsync("Phrase and Sentence");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ss.SpeakAsync(CurrentStop);
        ss.SpeakAsync("Phrase is a group of words without a complete thought.");
    }

请帮助我。这是我第一次使用c#以及system.speech

【问题讨论】:

    标签: c# speech-recognition text-to-speech speech speech-to-text


    【解决方案1】:

    使用GetCurrentlySpokenPromptSpeakAsyncCancel方法取消当前提示:

    var current = ss.GetCurrentlySpokenPrompt();
    
    if (current != null)
        ss.SpeakAsyncCancel(current);
    

    完整示例

    var ss = new SpeechSynthesizer();
    
    ss.SpeakAsync("This is a test.");
    
    Thread.Sleep(300);
    
    var current = ss.GetCurrentlySpokenPrompt();
    
    if (current != null)
        ss.SpeakAsyncCancel(current);
    
    ss.SpeakAsync("Goodbye.");
    

    【讨论】:

    • 您好,谢谢您的回复,但是我有办法使用var current = ss.GetCurrentlySpokenPrompt(); 进行全球吗?
    • 我不确定您所说的“全球”是什么意思。
    • 我尝试将Prompt current; 声明为全局变量,然后在我使用current = ss.GetCurrentlySpokenPrompt(); 之类的方法上,但它不起作用。也许我错了,我应该将什么替换为var 以使其成为全球性的。谢谢!
    • 当我说 global 时,我的意思是 public 。对此感到抱歉。嗯。你的例子很有效,但我想做的是这样public partial class WebForm2 { var current = ss.GetCurrentlySpokenPrompt(); //but it gives me error on this because I cannot use var outside a method, I'm trying to figure out what should I use here instead of var Button2_Click() { if (current != null) ss.SpeakAsyncCancel(current); ss.SpeakAsync("Goodbye."); } Button1_Click() { ss.SpeakAsync("This is a test."); Thread.Sleep(300); }
    • 你可以做一个属性:protected Prompt Current { get { return ss.GetCurrentlySpokenPrompt(); } }
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2017-09-19
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    相关资源
    最近更新 更多