2

我试图下载一个由谷歌翻译生成的MP3文件和虽然要达到这个,翻译并不如预期。C#下载文本到谷歌语音转换带有问题

我葡萄牙语和我们使用很多特殊字符的,我认为这是问题...

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"); 
      } 

的文件实际上创建的,但在这两种情况下的声音说同样的事情: OK直到“Teste de cria”(在'ç'和'〜'之前)并且确定为“没有ficheiro”。在中间的声音说的东西不是很明确... 希望是明确的。 =)

正如你所看到的,我尝试使用和不使用UrlEncode和相同的结果...我尝试UrlEncode所有url也。 我用BinaryWriter来试试,问题是一样的。 我尝试通过new Uri(url)myWebClient.DownloadFile并没有任何变化。

最让我恼火的是如果你把url结果放到你的浏览器中,你可以听到正确的文本到语音。 试试看:http://translate.google.com/translate_tts?tl=pt&q=Teste de criação no ficheiro

“Teste decriaçãono ficheiro”代表“文件创建测试”。

+0

这是因为网址错误的字符编码的可能。您的示例网址适用于浏览器。 url和url2是什么样的? – bzlm

+0

我认为WebClient.DownloadFile是英文的,并改变我的特殊字符为英文对应的。我检查了我的myWebClient.Encoding,但它附带“iso-8859-1:西欧(Windows)”正确的=( –

+0

我不知道更多要做什么,希望能帮助我或给予 –

回答

3

以下为我工作得很好:

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"); 
     } 
    } 
} 
+0

我认为'client.Headers [HttpRequestHeader.UserAgent] =“Mozilla/5.0(Windows NT 6.1; WOW64; rv:7.0.1)Gecko/20100101 Firefox/7.0.1”;'做魔术!非常感谢你! –