title: How to write audio stream out to response using SpeechSynthesizer C#
body: <p>I am using the SpeechSynthesizer class in a C# asp.net app to attempt to convert some text to speech and then write that audio memory stream out to the response so it can be downloaded as a .wav by the end user.</p>
  
  <p>I can save the memorystream to a filestream and the wav plays fine (tested this in a seperate function) however when trying to send a memorystream to the response (code below) it looks like IE says the .wav file is being downloaded however it is almost like something is hanging on to the object and won't let it complete downloading. The .wav as a result can  not be opened to play the sound.</p>
  
  <p>Any thoughts on how to properly do this? Here is my code below.</p>
  
  <pre><code>        SpeechSynthesizer synth = new SpeechSynthesizer();
          MemoryStream streamAudio = new MemoryStream();
  
          // Configure the synthesizer to output to an audio stream.
          synth.SetOutputToWaveStream(streamAudio);
  
          // Speak a phrase.
          synth.Speak("This is sample text-to-speech output.");
  
          // Set audio stream to beginning
          streamAudio.Position = 0;
  
          // Send the memory steam bytes out to the response/
          Response.Clear();
          Response.ContentType = "audio/wav";
          Response.AppendHeader("Content-Disposition", "attachment; filename=mergedoutput.wav");
          streamAudio.CopyTo(Response.OutputStream);
          Response.Flush();
          Response.End();
  </code></pre>