我在使用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和我想聽到的文字是中國。
更新:我用另一種方法來完成這項任務。如果有人感興趣。可以請我,並要求代碼。
你是如何編碼具有中國話的URL才能工作? – 2014-01-31 11:40:57