我正在尝试在 Unity3D 中调用 Microsoft Bing Text to Speech API 。
此API需要 accessToken 以及将在请求标头中传递的其他参数。 首先,为了获得 accessToken ,我已向令牌API 发送了POST请求,之后,我已在请求标头中发送了其他参数,它在 Postman 中完美地工作(返回Wav audioclip),如下所示:
为了在Unity中实现这一点,我使用 UnityWebRequest 类发送两个 POST请求(顺序),然后接收音频响应
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class TextToSpeech : MonoBehaviour
{
public AudioSource audioSource;
public static readonly string accessUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
public static readonly string synthesizeUri = "https://speech.platform.bing.com/synthesize";
public string textToSpeak = "Oh yeah! Finally Dorot is speaking.";
private static string accessToken;
private static readonly string apiKey = "MY API KEY";
private string postStringData;
public TextToSpeech (string textToSpeak)
{
this.textToSpeak = textToSpeak;
}
public void Speak()
{
audioSource = gameObject.GetComponent<AudioSource>();
StartCoroutine(RequestToken(apiKey));
}
private string GenerateSsml(string textToSpeak)
{
var ssmlDoc = new XDocument(
new XElement("speak",
new XAttribute("version", "1.0"),
new XAttribute(XNamespace.Xml + "lang", "en-US"),
new XElement("voice",
new XAttribute(XNamespace.Xml + "lang", "en-US"),
new XAttribute(XNamespace.Xml + "gender", "Male"),
new XAttribute("name", "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"),
textToSpeak)));
return ssmlDoc.ToString();
}
public IEnumerator RequestToken(string apiKey)
{
var tokenRequest = UnityWebRequest.Post(accessUri, "data");
tokenRequest.SetRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
var tokenResponse = tokenRequest.SendWebRequest();
yield return tokenResponse;
if (tokenRequest.isHttpError)
{
Debug.LogError("HTTP Error: " + tokenRequest.error + " Code: " + tokenRequest.responseCode);
}
else
{
postStringData = GenerateSsml(textToSpeak);
accessToken = tokenRequest.downloadHandler.text;
Debug.Log("Access token: " + accessToken);
StartCoroutine(Synthesize(postStringData, accessToken));
}
}
public IEnumerator Synthesize(string text, string token)
{
var synReq = UnityWebRequest.Post(synthesizeUri, text);
synReq.SetRequestHeader("Content-Type", "application/ssml+xml");
synReq.SetRequestHeader("X-Microsoft-OutputFormat", "riff-16khz-16bit-mono-pcm");
synReq.SetRequestHeader("X-Search-AppId", "07D3234E49CE426DAA29772419F436CA");
synReq.SetRequestHeader("X-Search-ClientID", "1ECFAE91408841A480F00935DC390960");
synReq.SetRequestHeader("User-Agent", "Dorot");
synReq.SetRequestHeader("Authorization", "Bearer " + token);
var synRes = synReq.SendWebRequest();
yield return synRes;
if (synReq.isHttpError)
{
Debug.LogError("HTTP Error: " + synReq.error + " Code: " + synReq.responseCode + " isNetworkError: " + synReq.isNetworkError + " isDone: " + synReq.isDone);
}
else
{
AudioClip cc = DownloadHandlerAudioClip.GetContent(synReq);
audioSource.clip = cc;
audioSource.Play();
}
}
}
结果,第一个API正确返回了令牌。但是,第二个返回的HTTP代码状态408 ,.像这样:
我该如何解决这个问题?感谢。
答案 0 :(得分:1)
找到解决方案。
根据this回答,UnityWebRequest
默认情况下chunkedTransfer
设置为true
。所以你需要这样做:
synReq.chunkedTransfer = false;
顺便说一句:您可以按照本指南为此编写一个单元测试:http://www.invisiblerock.com/unity-test-runner(这就是我尝试的方法)