0

in my asp.net project, I code below for a text to speech function. in my page's c# file.

 byte[] SpeakText(string text) 
        { 
            using (SpeechSynthesizer s = new SpeechSynthesizer()) 
            { 
                using (MemoryStream ms = new MemoryStream()) 
                { 
                    s.SetOutputToWaveStream(ms); 
                    s.Speak(text); 
                    return ms.GetBuffer(); 
                } 
            } 
        }
protected void Button1_Click(object sender, EventArgs e)
        {


            if (TextBox1.Text != "")
            {
                Response.Write(SpeakText(TextBox1.Text));

            }

        }

but not hear the audio while run it.

How to solve the problem?

C. Ross
  • 31,137
  • 42
  • 147
  • 238
bill qu
  • 3
  • 2

2 Answers2

2

Try setting the response content type:

Response.ContentType = "audio/wav";

Also don't use Response.Write as it expects encoded characters. You need to write binary to avoid getting corrupt data:

Response.Clear();
Response.ContentType = "audio/wav";
Response.BinaryWrite(SpeakText(TextBox1.Text));
Response.End();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    And add a `Response.End()` to skip adding html to that sound. – Hans Kesting Dec 05 '11 at 08:33
  • I have try but could hear the sound at the page. now my code is below: ' byte[] SpeakText(string text) { using (SpeechSynthesizer s = new SpeechSynthesizer()) { using (MemoryStream ms = new MemoryStream()) { s.SetOutputToWaveStream(ms); s.Speak(text); return ms.GetBuffer(); } } }' – bill qu Dec 06 '11 at 07:12
0

your code is executing on the server, not on the client machine.

Once you have generated the Speech output you should stream it down to the client (as a wav file for example), check this answer for a full example:

https://stackoverflow.com/a/1719867/559144

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • I have view the http://stackoverflow.com/a/1719867/559144 how to call ProcessRequest(HttpContext context) how to set value for context at asp.net button click function. – bill qu Dec 06 '11 at 07:14