Please help with the implementation of this example = Speech Synthesis with ASP.NET and HTML5

the WebForms application in the App_Code folder has created a class

using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.IO; using System.Linq; using System.Speech.Synthesis; using System.Threading; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; /// <summary> /// Сводное описание для SpeechSynthesizer /// </summary> [ConstructorNeedsTag(false)] public class SpeechSynthesizer : HtmlGenericControl, ICallbackEventHandler { private readonly System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer(); public SpeechSynthesizer() : base("audio") { this.Age = VoiceAge.NotSet; this.Gender = VoiceGender.NotSet; this.Culture = CultureInfo.CurrentCulture; this.VoiceName = String.Empty; this.Ssml = false; } [DefaultValue("")] public String VoiceName { get; set; } [DefaultValue(100)] public Int32 Volume { get; set; } [DefaultValue(0)] public Int32 Rate { get; set; } [TypeConverter(typeof(CultureInfoConverter))] public CultureInfo Culture { get; set; } [DefaultValue(VoiceGender.NotSet)] public VoiceGender Gender { get; set; } [DefaultValue(VoiceAge.NotSet)] public VoiceAge Age { get; set; } [DefaultValue(false)] public Boolean Ssml { get; set; } protected override void OnInit(EventArgs e) { AsyncOperationManager.SynchronizationContext = new SynchronizationContext(); var sm = ScriptManager.GetCurrent(this.Page); var reference = this.Page.ClientScript.GetCallbackEventReference(this, "text", String.Format("function(result){{ document.getElementById('{0}').src = result; document.getElementById('{0}').play(); }}", this.ClientID), String.Empty, true); var script = String.Format("\ndocument.getElementById('{0}').speak = function(text){{ {1} }};\n", this.ClientID, reference); if (sm != null) { this.Page.ClientScript.RegisterStartupScript(this.GetType(), String.Concat("speak", this.ClientID), String.Format("Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() {{ {0} }});\n", script), true); } else { this.Page.ClientScript.RegisterStartupScript(this.GetType(), String.Concat("speak", this.ClientID), script, true); } base.OnInit(e); } protected override void OnPreRender(EventArgs e) { this.Attributes.Remove("class"); this.Attributes.Remove("src"); this.Attributes.Remove("preload"); this.Attributes.Remove("loop"); this.Attributes.Remove("autoplay"); this.Attributes.Remove("controls"); this.Style[HtmlTextWriterStyle.Display] = "none"; this.Style[HtmlTextWriterStyle.Visibility] = "hidden"; base.OnPreRender(e); } public override void Dispose() { this.synth.Dispose(); base.Dispose(); } #region ICallbackEventHandler Members String ICallbackEventHandler.GetCallbackResult() { using (var stream = new MemoryStream()) { this.synth.Rate = this.Rate; this.synth.Volume = this.Volume; this.synth.SetOutputToWaveStream(stream); if (String.IsNullOrWhiteSpace(this.VoiceName) == false) { this.synth.SelectVoice(this.VoiceName); } else { this.synth.SelectVoiceByHints(this.Gender, this.Age, 0, this.Culture); } if (this.Ssml == false) { this.synth.Speak(this.Context.Items["data"] as String); } else { this.synth.SpeakSsml(this.Context.Items["data"] as String); } return (String.Concat("data:audio/wav;base64,", Convert.ToBase64String(stream.ToArray()))); } } void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument) { this.Context.Items["data"] = eventArgument; } #endregion } 

added test page

 <%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeFile="speech.aspx.cs" Inherits="contact_speech" %> <%@ Register Assembly="System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Speech" TagPrefix="web" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function onSpeak(text) { document.getElementById('synthesizer').speak(text); } </script> </head> <body> <form id="form1" runat="server"> <div> <div> <web:SpeechSynthesizer runat="server" ID="synthesizer" Age="Adult" Gender="Male" Culture="en-US" Rate="0" Volume="100" /> <input type="text" id="text" name="text" /> <input type="button" value="Speak" onclick="onSpeak(this.form.text.value)" /> </div> <%--<asp:Button ID="btVoice" runat="server" Text="ОЗВУЧИТЬ СОДЕРЖИМОЕ СТРАНИЦЫ" OnClick="btVoice_Click" />--%> </div> </form> </body> </html> 

when building an application, I get an error

Warning 1 Unknown element "SpeechSynthesizer". This can occur if there is a compilation error on the website or the web.config file is missing. \ speech.aspx 21 22

please tell me how to fix the error Regards Yuri

    1 answer 1

    Do not use the App_Code folder (and the namespace Code - tode). This is the system folder for the old type of projects - Web Sites, which were compiled completely on the fly. There may be problems with the visibility of classes from it.

    How to bring your example into working view:

    1. Transfer the file with the control from the folder App_Code to the folder with a neutral name, for example Controls.
    2. Check in the properties of the file Build Action - if it is set in Content - change to Compile.
    3. Set the namespace for the class, for example:

       namespace WebApplication9.Controls { ... [ConstructorNeedsTag(false)] public class SpeechSynthesizer : HtmlGenericControl, ICallbackEventHandler { ... } } 
    4. Register your namespace with the prefix web: in web.config:

       <system.web> <pages> <controls> <add assembly="WebApplication9" namespace="WebApplication9.Controls" tagPrefix="web"/> </controls> </pages> 
    5. Remove control from the page. Save it.
    6. Rebuild the project.
    7. Return control to the page. A page view will appear in the page's designer file.

       protected global::WebApplication9.Controls.SpeechSynthesizer synthesizer; 
    8. Launch the app and enjoy.

    For a project like Web Site (without csproj and compiled on the fly):

    1. Leave the control file in App_Code, but add a namespace.
    2. In web.config, register the namespace without specifying assembly:

       <add namespace="WebApplication9.Controls" tagPrefix="web"/> 

    But in general - Web Forms is slowly dying. Look towards MVC.

    • That's just the point: the project is a WebForms application (the old type of project is Web Sites). How for this type of site to realize this opportunity? What you have described gives the same error ... Switch to MVC - if this is the only way out of this situation - please send me an email to (krakoss@mail.ru) this project implemented at MVC (if it doesn’t bother you) - I’ll be a long time to implement. Regards, Yuri - Yuri Kosenko
    • Those. you have a very, very old project, for the 2005 studio, from which MS got rid of back in 2008? Or you have a Web Application with Web Forms - i.e. is there a csproj file that you open in the studio? The solution from the answer is for the csproj / web application. If it didn’t work for you, then you missed a step, or you made a mistake somewhere with the namespace or assembly name. Pour somewhere sources, or give a minimally reproducible example in the question. - PashaPash
    • ( cloud.mail.ru/public/5yzA/Zb7euybC3 ) - laid out on Mayle ... But in general - Web Forms is slowly dying. Look in the direction of MVC ... exactly the task at hand - I will study - Yuri Kosenko
    • ( cloud.mail.ru/public/LFCS/EgUSsWpUJ ) these are my websites - Yuri Kosenko
    • @Yuri Kosenko took your project (by the first link), returned the control to App_Code, removed the reference from the assembly from the web.config - and the control picked up normally on the page. There is no sound, but this is already some other problem. - PashaPash