Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 12-Jul-11 0:12am
Edited 12-Jul-11 0:26am
Slacker00770.8K
v2
Comments
Slacker007 at 12-Jul-11 6:26am
   
Edited for general readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
<!-- 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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Richard Deeming 135
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100