【问题标题】:Issue in creating demo of text to speech using speech synthesizer and spVoice使用语音合成器和 spVoice 创建文本到语音演示的问题
【发布时间】:2023-04-10 08:21:01
【问题描述】:

我正在制作一个文本到语音的演示,其中我正在使用语音合成器。 我的问题是当我单击播放按钮时,页面正在连续加载。 即使演讲结束,它也不会停止。在我的演示中,暂停和恢复也不起作用。

我也尝试使用 spVoice 界面进行文本转语音,但在此演示中,暂停和恢复也不起作用。

使用语音合成器的演示 -

SpeechSynthesizer spRead;
protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
       {
          // Creating new object of SpeechSynthesizer.
          spRead = new SpeechSynthesizer();
       }    

} // Page_Load
protected void btnPlay_Click(object sender, EventArgs e)
{  
    // Get the content data as per the content id  
    _contentData = new ContentFormData(ContentManager.GetContentData(Page.Database, ContentId, Page.IsLive));

    // Get the text after trim
    _speechText = WebUtility.HtmlDecode(_contentData.Content.Text1.Trim());

    // If Speech Text is not null
    // then check the button class, if cssclass is play change it to pause 
       and call speak method.
    if (_speechText != null && !string.IsNullOrEmpty(_speechText))
    {
        // if button is play buttton 
        // then call play method and speech the text
        if (btnPlay.CssClass == "button-play")
        {
            btnPlay.CssClass = btnPlay.CssClass.Replace("button-play", 
            "button-pause");
            // creating the object of SpeechSynthesizer class  
            spRead = new SpeechSynthesizer();
            spRead.SpeakAsync(_speechText);
            spRead.SpeakCompleted += new 
            EventHandler<SpeakCompletedEventArgs>(spRead_SpeakCompleted);
        }
        // If button class is pause
        // then change it to continue and call pause method.
        else if (btnPlay.CssClass == "button-pause")
        {
            btnPlay.CssClass = btnPlay.CssClass.Replace("button-pause", 
            "button-continue");
            if (spRead != null)
            {
                // Check the state of spRead, and call pause method.
                if (spRead.State == SynthesizerState.Speaking)
                {
                    spRead.Pause();
                }
            }
            btnPlayFromStart.Enabled = true;
        }
        // If button class is continue
        // then change it to pause and call resume method.
        else if (btnPlay.CssClass == "button-continue")
        {
            btnPlay.CssClass = btnPlay.CssClass.Replace("button-continue", 
            "button-pause");
            if (spRead != null)
            {
                // Check the state of spRead, and call resume method.
                if (spRead.State == SynthesizerState.Paused)
                {
                    spRead.Resume();
                }
            }
            btnPlayFromStart.Enabled = false;
        }
    }

}

private void spRead_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
    // If Spread is not null
    // then dispose the spread after the speak is completed  
    // else do nothing
    if (spRead != null)
    {
        spRead.Dispose();
    }
    else
    {
        // do nothing
    }
} // spRead_SpeakCompleted

使用 SpVoice 演示 -

SpVoice voice;
protected void Page_Load(object sender, EventArgs e)
{
    _contentData = new 
    ContentFormData(ContentManager.GetContentData(Page.Database, ContentId, 
    Page.IsLive));

    _speechText = WebUtility.HtmlDecode(_contentData.Content.Text1.Trim());

} // Page_Load

protected void btnPlay_Click(object sender, EventArgs e)
{
    voice = new SpVoice();
    if (btnPlay.CssClass == "button-play")
    {
        voice.Speak(_speechText, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        btnPlay.CssClass = btnPlay.CssClass.Replace("button-play", "button-
        pause");
    }
    else if (btnPlay.CssClass == "button-pause")
    {
        voice.Pause();
        btnPlay.CssClass = btnPlay.CssClass.Replace("button-pause", "button-
        continue");
    }
    else if (btnPlay.CssClass == "button-continue")
    {
        voice.Resume();
        btnPlay.CssClass = btnPlay.CssClass.Replace("button-continue", 
        "button-play");
    }

}

【问题讨论】:

  • 您需要在页面加载中的!isPostback 中调用`spRead = new SpeechSynthesizer();`,与spvoice 相同
  • 感谢 Webruster 的回复尝试了这个,但还是不行。
  • 你能用你尝试过的最新代码更新你的问题吗
  • 你需要为Using SpVoice做同样的事情
  • 当我放语音 = new SpVoice();在 !isPostback 内,单击播放按钮时,我在 speak 方法上收到对象空引用错误。但是当我把它放在外面时 isPostback 说话方法工作正常,但暂停和恢复不起作用。

标签: c# asp.net speechsynthesizer


【解决方案1】:

通过使用处理程序解决了该问题,以停止回发。 将语音对象存储在会话中,并在暂停和恢复时从会话中获取语音对象。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2015-07-09
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多