2012-03-30 97 views
2

我在使用Microsoft Translator Speak方法的HTTP接口時遇到了一些問題。客戶端使用Microsoft Translator沒有聲音

這是我的代碼:

public void Speak() 
    { 
     AdmAuthentication admAuth = new AdmAuthentication("clientID", "clientSecret"); 
     AdmAccessToken admToken; 
     string headerValue; 
     admToken = admAuth.GetAccessToken(); 
     // Create a header with the access_token property of the returned token 
     headerValue = "Bearer " + admToken.access_token; 
     string language = "zh-CHS"; 
     string uri = "https://api.microsofttranslator.com/v2/Http.svc/Speak?&text=" + System.Web.HttpUtility.UrlEncode(lblRead.Text) + "&language=" + language + "&format=" + HttpUtility.UrlEncode("audio/wav") + "&options=MaxQuality"; 
     WebRequest webRequest = WebRequest.Create(uri); 
     webRequest.Headers.Add("Authorization", headerValue); 
     WebResponse response = null; 
     try 
     { 
      response = webRequest.GetResponse(); 
      using (Stream stream = response.GetResponseStream()) 
      { 
       using (SoundPlayer player = new SoundPlayer(stream)) 
       { 
        player.PlaySync(); 
       } 
      } 
     } 
     catch 
     { 

      throw; 
     } 
     finally 
     { 
      if (response != null) 
      { 
       response.Close(); 
       response = null; 
      } 
     } 
    } 
    } 

    public class AdmAccessToken 
    { 
     [DataMember] 
     public string access_token { get; set; } 
     [DataMember] 
     public string token_type { get; set; } 
     [DataMember] 
     public string expires_in { get; set; } 
     [DataMember] 
     public string scope { get; set; } 
    } 

    public class AdmAuthentication 
    { 
     public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; 
     private string clientId; 
     private string cientSecret; 
     private string request; 

     public AdmAuthentication(string clientId, string clientSecret) 
     { 
      this.clientId = clientId; 
      this.cientSecret = clientSecret; 
      //If clientid or client secret has special characters, encode before sending request 
      this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret)); 
     } 

     public AdmAccessToken GetAccessToken() 
     { 
      return HttpPost(DatamarketAccessUri, this.request); 
     } 

     private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails) 
     { 
      //Prepare OAuth request 
      WebRequest webRequest = WebRequest.Create(DatamarketAccessUri); 
      webRequest.ContentType = "application/x-www-form-urlencoded"; 
      webRequest.Method = "POST"; 
      byte[] bytes = Encoding.ASCII.GetBytes(requestDetails); 
      webRequest.ContentLength = bytes.Length; 
      using (Stream outputStream = webRequest.GetRequestStream()) 
      { 
       outputStream.Write(bytes, 0, bytes.Length); 
      } 
      using (WebResponse webResponse = webRequest.GetResponse()) 
      { 
       DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken)); 
       //Get deserialized object from JSON stream 
       AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream()); 
       return token; 
      } 
     } 

我刪除了客戶端ID和客戶端密鑰。

我面臨的問題是,如果我在服務器上運行站點,我無法聽到任何聲音。是的,我知道當用戶點擊一個按鈕時,SoundPlayer就會在服務器上產生,因此客戶端聽不到任何聲音。

我搜索了各種搜索方法。但無濟於事。

我試過使用一種方法來保存流並更新到數據庫。一切都很好,如果我從Visual Studio中運行它。但在客戶端,它無法下載流。甚至播放從數據庫中檢索的聲音文件。

請幫我

我使用Visual Studio 2010和.NET框架4.0和我想聽到的文字是中國。

更新:我用另一種方法來完成這項任務。如果有人感興趣。可以請我,並要求代碼。

+0

你是如何編碼具有中國話的URL才能工作? – 2014-01-31 11:40:57

回答

0

你實際上可以在Silverlight中端到端實現這個過程,但是我不知道如何在ASP.NET中使用只有。但是,由於您可以在ASP.NET頁面上輕鬆定位Silverlight控件(請參閱here),您可以將轉換代碼API調用重構爲一個小的Silverlight控件,並將其放置在ASP.NET頁面上。

正如Tim Heuer的博客文章中指出的,MediaElement不能直接播放無損WAV格式。然而Codeplex上有一個'WaveMediaStreamSource'對象,它由Silverlight團隊的成員創建,並允許您在需要時在客戶端上播放流。

在通過REST調用翻譯器API(或您選擇的任何一種方法:SOAP,HTTP,AJAX)接收到trasalated值之後,可以加載WaveMediaStreamSource構造器並將其值重放到客戶端。

如果您以前從未使用過Silverlight,請不要害怕,因爲此解決方案開始結束應該不會那麼困難。下面是Tim的示例和CodePlex的關聯WaveMediaStreamSource的鏈接。

Make your Silverlight applications Speak to you with Microsoft Translator

WaveMediaStreamSource (Codeplex)

+1

Atconway。謝謝你的幫助。最後,我導致使用HTML5在客戶端瀏覽器上獲取我的音頻。謝謝=) – Respect 2012-03-31 03:49:32

+0

同意。作爲一個可行的解決方案,HTML5將會爲此需求而招攬Silverlight,並且無論如何都是更可行的選擇。 – atconway 2012-04-02 15:14:24