在Unity中,有的时候需要用到语音服务。除了百度、飞讯外,Bing的语音服务其实也不错。
private IEnumerator GetSound(string text, AudioClip play)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("ContentType", "application/x-www-form-urlencoded");
headers.Add("Ocp-Apim-Subscription-Key", );
WWW postData = new WWW(@"https://api.cognitive.microsoft.com/sts/v1.0/issueToken", new byte[] { 0 }, headers);
while (!postData.isDone) yield return new WaitForSeconds(0.1f);
string kAu = postData.text;
headers = new Dictionary<string, string>();
headers.Add("ContentType", "application/ssml+xml");
headers.Add("X-Microsoft-OutputFormat", "riff-16khz-16bit-mono-pcm");
headers.Add("User-Agent", "VirtualShopping");
headers.Add("Authorization", "Bearer " + kAu);
string postdata = "<speak version='1.0' xml:lang='zh-CN'><voice xml:lang='zh-CN' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)'>" + text + "</voice></speak>";
WWW getSound = new WWW(@"https://speech.platform.bing.com/synthesize", Encoding.UTF8.GetBytes(postdata), headers);
while (!getSound.isDone) yield return new WaitForSeconds(0.1f);
play = getSound.GetAudioClip(true, false, AudioType.WAV);
}
只需要替换掉Bing语音的Key就可以直接把这段代码复制进去用了。没错写的不是很简洁,但是能用就行了,谁管那么多呢。。。