1

I am trying to be able to download mp3's from the google translate text to speak API. My code works with english but not with Japanese characters. The downloaded audio files are silent. Does anyone know what I'm doing wrong?

I used this post for reference, but he worked with German instead of Japanese.

using (var client = new WebClient())
        {
            client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            client.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            client.Headers.Add("Accept-Language", "ja-JP,ja;q=0.8,en-US;q=0.6,en;q=0.4");
            client.Encoding = System.Text.Encoding.UTF8;

            client.DownloadFile("http://translate.google.com/translate_tts?tl=ja&q=日本語", @"C:\test.mp3");
        }
Community
  • 1
  • 1
Howell21
  • 151
  • 2
  • 9

1 Answers1

2

I've never worked with that API, but I suggest you encode the URL you're passing to Client.DownloadFile with System.Uri.EscapeDataString(). Those japanese characters in an URL string don't look any good.

Here's the solution, it works :). It seems to me you were missing the User-Agent, Accept-Language and removing the client.Encoding part.

        using (var client = new WebClient())
        {
            client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");
            client.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
            client.Headers.Add("Accept-Language", "en-US,en;q=0.8");
            //client.Encoding = System.Text.Encoding.UTF8;

            client.DownloadFile("http://translate.google.com/translate_tts?tl=ja&q=%E6%97%A5%E6%9C%AC%E8%AA%9E", @"test.mp3");
        }
DARKGuy
  • 843
  • 1
  • 12
  • 34