在Unity3D中使用Bing语音服务(文字转语音)

在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", /*你的必应语音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);
        //https://docs.microsoft.com/zh-cn/azure/cognitive-services/Speech/api-reference-rest/bingvoiceoutput 参考资料
        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就可以直接把这段代码复制进去用了。没错写的不是很简洁,但是能用就行了,谁管那么多呢。。。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值