0

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();
            }
        }
    }
Bertie
  • 1
  • 1

3 Answers3

0
        string GoogleTranslateURL = @"http://translate.google.com/translate_tts?tl=en&q=pop";
        System.Net.WebRequest req = System.Net.WebRequest.Create(GoogleTranslateURL);

        string a = req.GetResponse().ContentType;


        using (Stream webStream = req.GetResponse().GetResponseStream())
        {
            FileStream stream = new FileStream(@"C:\Projects\EverydayProject\pop.wav", FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite, 100, FileOptions.None);

            webStream.CopyTo(stream);
            stream.Close();
            webStream.Close();
        }


        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
        startInfo.Arguments = @"C:\Projects\EverydayProject\pop.wav";
        var process = Process.Start(startInfo);

        if(!process.HasExited)
        {
            process.Refresh();
            Thread.Sleep(1000);
        }

        process.CloseMainWindow();
        process.Close();

        if (File.Exists(@"C:\Projects\EverydayProject\pop.wav"))
            File.Delete(@"C:\Projects\EverydayProject\pop.wav");

The problem is not in the HTTP Request ! This is working for me, the problem is in the SoundPlayer class. Check how to work with SoundPlayer.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • I don't think is good, because `SoundPlayer` can only play wav, not mp3. Moreover what if OP doesn't have VLC? – Tony Oct 05 '14 at 01:54
  • @Tony op thinks that the problem is in the Http request. Request return valid wav file which is working with other programs. Because of that I post this. He should see how SoundPlayer is working. Error comes from SoundPlayer. If he doesn't have VLC he can use whatever he want. – mybirthname Oct 05 '14 at 02:08
  • @Tony you are right SoundPlayer can play only wav and content type of the response is not wav ! – mybirthname Oct 05 '14 at 02:17
0

SoundPlayer can play WAV files only, here what msdn said:

The SoundPlayer class provides a simple interface for loading and playing a .wav file.

But google replies with mp3 file, so try MediaPlayer instead:

var mp = new MediaPlayer();
mp.Open(new Uri(GoogleTranslateURL + (string.IsNullOrEmpty(Query) ? "" : "&q=" + Query)));
mp.Play();
Tony
  • 7,345
  • 3
  • 26
  • 34
  • MediaPlayer doesn't have constructor without parameters. You need to add byte[]. You should probably fix the code or I'm somehow mistaken. – mybirthname Oct 05 '14 at 02:16
  • I tried your code I put byte[] in the MediaPlayer and I had an exception. – mybirthname Oct 05 '14 at 02:30
  • @mybirthname Sorry, but it has: http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.mediaplayer(v=vs.110).aspx I think you might use some other `MediaPlayer` class. – Tony Oct 05 '14 at 03:30
-1

Try:

Player.Stream.Position = 0;

cesaraviles
  • 154
  • 1
  • 4