使用语音合成器和spVoice创建文本到语音演示的问题

时间:2017-05-09 04:45:46

标签: c# asp.net speechsynthesizer

我正在进行文本到语音演示,我正在使用语音合成器。 我的问题是,当我点击播放按钮时,页面会不断加载。 即使演讲结束,它也不会停止。我的演示暂停和恢复也无法正常工作。

我还尝试使用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");
    }

}

1 个答案:

答案 0 :(得分:0)

通过使用handeler解决问题,停止回发。 在会话中和暂停中存储语音对象,并从会话中恢复获取语音对象。