Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm creating a chatbot web application using asp and c#. I am having a little trouble using Text-to-speech using SpeechLib, after the page loads, it speaks out the word before putting it to the textbox, but it should be textbox first then speak.

I think the conflict is on the scriptmanager, but not quite sure, can anyone please help me.. my codes are:

c# codes (default.cs)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SpeechLib;

namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onclick","");
            TextBox2.Focus();
        }
        private void speakNow(string text)
        {
            try
            {
                SpVoice voice = new SpVoice();
                voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            TextBox1.Text += "\n" + TextBox2.Text;
            ScriptManager man = ScriptManager.GetCurrent(Page);
            ScriptManager.RegisterClientScriptBlock(
                Button1, this.GetType(), "scrollDown", "var objDiv = document.getElementById(\"TextBox1\");objDiv.scrollTop = objDiv.scrollHeight;",true);
            speakNow(TextBox2.Text);
            TextBox2.Text = "";
            TextBox2.Focus();  
            
        }
    }
}


default.aspx
<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body onload = "scrollDown()">
    <form id="form1" runat="server">
    <script type = "text/javascript">
    function scrollDown()
    {

    }
    </script>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <script type = "text/javascript">

        </script>
            <asp:TextBox ID="TextBox1" runat="server" Height="137px" TextMode="MultiLine"
                Width="230px"></asp:TextBox>
            <br />
            <asp:TextBox ID="TextBox2" runat="server" Width="173px"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
                Text="Button" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

Posted
Updated 12-Jul-11 0:26am
v2
Comments
Slacker007 12-Jul-11 6:26am    
Edited for general readability.

SpVoice voice = new SpVoice();
voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);

This runs on your server, not your client. It can only run BEFORE your page is served, for that reason, but your client will never hear it. ASP.NET can't do what you want, you need to embed a client side control to do it.
 
Share this answer
 
I realize this is an old question but here is an answer anyway.

This is from Microsoft and is their demo SimpleTTS.html It only runs in iExplorer and you will have to enable activeX under Tools, Internet options, Security.

The viseme images are simply little mouth images selected according to the viseme change events to make it look like the mouth is talking. Microsoft no longer has this code on MSDN as far as I can tell. Tooo bad.

You can massage this to do what you want it to do. Good luck.

HTML
<!-- Copyright @ 2001 Microsoft Corporation All Rights Reserved. -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TTS Demo</title>

<script language="JavaScript">

// Create the Sapi SpVoice object
var VoiceObj = new ActiveXObject("Sapi.SpVoice");

// ChangeVoice() function:
// 		This function sets the newly selected voice choice from the Voice 
//		Select box on the Voice object.
function ChangeVoice() {
	var i = parseInt( idsVoices.value );
	VoiceObj.Voice = VoiceObj.GetVoices().Item(i);
}

// ChangeAudioOutput() function:
//		This function sets the newly selected audio output choice from the
//		Audio Output Select box on the Voice object.
function ChangeAudioOutput() {
	var i = parseInt( idsAudioOutputs.value );
	VoiceObj.AudioOutput = VoiceObj.GetAudioOutputs().Item(i);
}

// IncRate() function:
//		This function increases the speaking rate by 1 up to a maximum
//		of 10.
function IncRate() {
	if( VoiceObj.Rate < 10 )
	{
		VoiceObj.Rate = VoiceObj.Rate + 1;
	}
}

// DecRate() function:
//		This function decreases the speaking rate by -1 down to a minimum
//		of -10.
function DecRate() {
	if( VoiceObj.Rate > -10 )
	{
		VoiceObj.Rate = VoiceObj.Rate - 1;
	}
}

// IncVol() function:
//		This function increases the speaking volume by 10 up to a maximum
//		of 100.
function IncVol() {
	if( VoiceObj.Volume < 100 )
	{
		VoiceObj.Volume = VoiceObj.Volume + 10;
	}
}

// DecVol() function:
//		This function decreases the speaking volume by -10 down to a minimum
//		of 0.
function DecVol() {
	if( VoiceObj.Volume > 9 )
	{
		VoiceObj.Volume = VoiceObj.Volume - 10;
	}
}

// SpeakText() function:
//		This function gets the text from the textbox and sends it to the
//		Voice object's Speak() function. The value "1" for the second
//      		parameter corresponds to the SVSFlagsAsync value in the SpeechVoiceSpeakFlags
//		enumerated type.
function SpeakText() {
	if( idbSpeakText.value == "SpeakText" )
	{
		// Speak the string in the edit box
		try
		{
			VoiceObj.Speak( idTextBox.value, 1 );
		}
		catch(exception)
		{
			alert("Speak error");
		}
	}
	else if( idbSpeakText.value == "Stop" )
	{
		// Speak empty string to Stop current speaking. The value "2" for 
		// the second parameter corresponds to the SVSFPurgeBeforeSpeak
		// value in the SpeechVoiceSpeakFlags enumerated type.
		VoiceObj.Speak( "", 2 );
	}
}

</script>

<script for="window" event="OnQuit()" language="JavaScript">
	// Clean up voice object
	delete VoiceObj;
</script>

</meta></head>

<body>
<h1 align="center">Simple TTS (DHTML)</h1>
<h1 align="center"><font size="3">        </font>
<img alt="" border="2" hspace="0" id="idImage" src="mouthclo.bmp">  </img></h1>
<h1 align="center">  
<textarea id="idTextBox" cols="50" rows="10" wrap="VIRTUAL">Enter text you wish spoken here</textarea>
</h1>

<p align="center"> 
Rate  
<input id="idbIncRate" name="button1" type="button" onclick="IncRate()" value="    +    "></input> 
<input id="idbDecRate" name="button2" type="button" onclick="DecRate()" value="    -    " style="LEFT: 237px; TOP: 292px">     </input>  

Volume  
<input id="idbIncVol" name="button3" onclick="IncVol()" style="LEFT: 67px; TOP: 318px" type="button" value="    +    "> 
<input id="idbDecVol" name="button4" onclick="DecVol()" type="button" value="    -    " style="LEFT: 134px; TOP: 377px">
</input></input></p>
 
<p align="center"><button id="idbSpeakText" onclick="SpeakText();">
   style="HEIGHT: 24px; LEFT: 363px; TOP: 332px; WIDTH: 178px">SpeakText</button></p>
 
<p align="center">Voice                                             
   Audio Output </p>
<p align="center"> 

<select id="idsVoices" name="Voices" onchange="ChangeVoice()" style="FONT-FAMILY: serif; HEIGHT: 21px; WIDTH: 179px"> </select>
            

<select id="idsAudioOutputs" name="AudioOutputs" onchange="ChangeAudioOutput()" style="HEIGHT: 22px; WIDTH: 179px">  </select>
 

<script language="JavaScript">
// Code in the BODY of the webpage is used to initialize controls and
// to handle SAPI events

/***** Initializer code *****/
InitializeControls();

function InitializeControls()
{
	// Initialize the Voices and AudioOutput Select boxes
	var VoicesToken = VoiceObj.GetVoices();
	var AudioOutputsToken = VoiceObj.GetAudioOutputs();

	// Add correct strings to Voice Select box
	for( var i=0; i<voicestoken.count;>	{
		var oOption = document.createElement("OPTION");
		idsVoices.options.add(oOption);
		oOption.innerText = VoicesToken.Item(i).GetDescription();
		oOption.value = i;
	}

	// Add correct strings to Audio Output Select box
	for( var i=0; i<audiooutputstoken.count;>	{
		var oOption = document.createElement("OPTION");
		idsAudioOutputs.options.add(oOption);
		oOption.innerText = AudioOutputsToken.Item(i).GetDescription();
		oOption.value = i;
	}	
}

/***** Event handling code *****/
// These functions are used to handle the SAPI events

// Handle StartStream event	
function VoiceObj::StartStream() {
	idbSpeakText.value = "Stop";
}

// Handle EndStream event	
function VoiceObj::EndStream() {
	idbSpeakText.value = "SpeakText";
	idImage.src = "mouthclo.bmp";
}

// Handle Viseme event	
function VoiceObj::Viseme(StreamNum, StreamPos, Duration, VisemeType, Feature, VisemeId) {
	// Map the VisemeId to the appropriate .bmp
	if( VisemeId == 15 || VisemeId == 17 || VisemeId == 18 || VisemeId ==21 )
	{
		idImage.src = "mouthop1.bmp";
	}
	else if( VisemeId == 14 || VisemeId == 16 || VisemeId == 19 || VisemeId == 20 )
	{
		idImage.src = "mouthop2.bmp";
	}
	else if( VisemeId == 4 || VisemeId == 6 || VisemeId == 9 || VisemeId == 12 )
	{
		idImage.src = "mouthop3.bmp";
	}
	else if( VisemeId == 1 || VisemeId == 2 || VisemeId == 3 || VisemeId == 11 )
	{
		idImage.src = "mouthop4.bmp";
	}
	else if( VisemeId == 7 || VisemeId == 8 )
	{
		idImage.src = "mouthnar.bmp";
	}
	else if( VisemeId == 5 || VisemeId == 10 || VisemeId == 13 )
	{
		idImage.src = "mouthmed.bmp";
	}
	else
	{
		idImage.src = "mouthclo.bmp";
	}
}
</script>


<hr></hr>
<p></p>

</p></body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900