Frage

I'm trying to download a mp3 file generated by google translate and although to achieve this, the translation doesn't come as expected.

I'm portuguese and we use lots of special chars and I think that is the problem...

string text = "Teste de criação no ficheiro";
            string googleTextToSpeech = "http://translate.google.com/translate_tts?tl=pt&q=";
            string url = googleTextToSpeech + HttpUtility.UrlEncode(text);
            string url2 = googleTextToSpeech + text;

using (WebClient myWebClient = new WebClient())
            {
                myWebClient.DownloadFile(url, pathToSaveFile + "\\" + "mp3CriationTest.mp3");
                myWebClient.DownloadFile(url2, pathToSaveFile + "\\" + "mp3CriationTest2.mp3");
            }

The files are actually created but in both cases the voice says the same thing: ok until "Teste de cria" (before 'ç' and '~') and ok with "no ficheiro". In the middle voice says something not very explicit... Hope I was explicit. =)

As you can see I try with and without UrlEncode and same result... I try UrlEncode all url too. I try it with BinaryWriterand the problem is equal. I try it passing new Uri(url) in myWebClient.DownloadFile and nothing changes.

The thing that most annoys me is if you put the url result in your browser you can listen the right text to speech. Try it:http://translate.google.com/translate_tts?tl=pt&q=Teste de criação no ficheiro

"Teste de criação no ficheiro" stands for "File creation test".

War es hilfreich?

Lösung

The following works fine for me:

using System.Net;
using System.Text;
using System.Web;

class Program
{
    static void Main()
    {
        var text = "Teste de criação no ficheiro";
        var url = "http://translate.google.com/translate_tts?tl=pt&q=";
        url += HttpUtility.UrlEncode(text, Encoding.GetEncoding("utf-8"));
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
            client.DownloadFile(url, "mp3CriationTest.mp3");
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top