0

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

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

}
4

1 回答 1

0

Solved the issue by using handeler, to stop the postback. Stored the voice object in session and in pause and resume get the voice object from session.

于 2017-05-10T05:01:42.253 回答