要怎麼讓電腦說話
使用Microsoft Translator Speaker API
Jing (mqJing@gmail.com)
預計學習時間: 10 分鐘
這是一個 AP , 用來展示如何向微軟存取服務的 token. 以及如何搭配這個 token 呼叫語音合成 method 的例子.
Download
Outline
- 註冊 Azure Marketplace 你的應用程式
- 開始寫程式
註冊 Azure Marketplace 你的應用程式
Step 1: 註冊 Azure Marketplace
開始寫程式
Step 1: Add reference libraries. [Add Reference] -> [.NET]
- System.Runtime.Serialization .dll for System.Runtime.Serialization.Json
- System.ServiceModel .dll for System.ServiceModel.Channels
- 把 [C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll] 加進來 (一定要
System.Runtime Serialization
System.ServiceModel
System.Web (請使用 2.0.50727)
Step 2: 取得 authentication token
| // 查閱 client id & client secret: // https://datamarket.azure.com/developer/applications/ |
| // 輔助 class: 幫我們利用 Post 的方式, 向微軟取得 access token // 申請窗口: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; |
Step 3: 使用 Speak method 搭配著 token, 要求說話.
| // Speak API 介面: http://api.microsofttranslator.com/v2/Http.svc/Speak // authToken= "Bearer " + <剛剛得到的 token 字串>; private static void Speak(string authToken){ string strtext = “井民全”; string string strLanguage="zh-tw"; string uri = http://api.microsofttranslator.com/v2/Http.svc/Speak?appId=&text= +HttpUtility.UrlEncode(strtext) + "&language=" + strLanguage; // 組合 Speak 指令 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Headers.Add("Authorization", authToken); // 送出指令 response = httpWebRequest.GetResponse(); // 建立媒體播放器, 播放 Server 回傳的合成語音 using (Stream stream = response.GetResponseStream()) { using (SoundPlayer player = new SoundPlayer(stream)) { player.PlaySync(); } } } // end of speak method |
References:
- 查閱 client id & client secret, https://datamarket.azure.com/developer/applications/
- Obtaining an Access Token, http://msdn.microsoft.com/en-us/library/hh454950.aspx
- Speak Method, http://msdn.microsoft.com/en-us/library/ff512420.aspx
- http://stackoverflow.com/questions/9937421/no-sound-from-client-side-using-microsoft-translator