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」;'做魔術!非常感謝你! –