ASP.net文本到语音帮助 [英] ASP.net text-to-speech help

查看:153
本文介绍了ASP.net文本到语音帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp和c#创建一个聊天机器人Web应用程序.我在使用SpeechLib使用文本转语音"时遇到了一些麻烦,在页面加载后,它会先说出单词,然后再将其放入文本框,但应该先是文本框然后再说.

我认为脚本管理器存在冲突,但不太确定,谁能帮助我..我的代码是:

c#代码(default.cs)

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



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>


推荐答案

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

它在您的服务器上运行,而不是在客户端上运行.因此,它只能在提供您的页面之前运行.但是,您的客户端将永远不会听到它. ASP.NET不能做您想做的事,您需要嵌入一个客户端控件来做它.
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.


我知道这是一个老问题,但是无论如何这是一个答案.

这是来自Microsoft的演示SimpleTTS.html.它仅在iExplorer中运行,您必须在工具","Internet选项",安全性"下启用activeX.

视位素图像仅仅是根据视位素变化事件选择的小嘴图像,以使其看起来像嘴巴在说话.据我所知,Microsoft在MSDN上不再有此代码.太糟糕了.

您可以按摩它以完成您想做的事情.祝你好运.

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>


这篇关于ASP.net文本到语音帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆