I'm very new at C# and I can't seem to find the issue with my code. I am trying to get an audio file from the Google Translate text-to-speech site and play it, but I keep getting the error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: The wave header is corrupt.
Could someone let me know what the issue is in my code? The method is:
public static void PlayWord(string Query)
{
string GoogleTranslateURL = "http://translate.google.com/translate_tts?tl=en";
System.Net.WebRequest req = System.Net.WebRequest.Create(GoogleTranslateURL + (string.IsNullOrEmpty(Query) ? "" : "&q=" + Query));
using (var ms = new MemoryStream())
{
using (Stream webStream = req.GetResponse().GetResponseStream())
{
var buffer = new byte[4096];
int read;
while (webStream != null && (read = webStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
}
using (SoundPlayer player = new SoundPlayer(ms))
{
if (ms.CanSeek) ms.Seek(0, System.IO.SeekOrigin.Begin);
player.Stream = null;
player.Stream = ms;
player.Play();
}
}
}