我的代码最初只返回文本响应。我尝试使用---------------------------------------增加音频响应。检测意图响应 但不确定我的设置是否正确?
UnityWebRequest postRequest = new UnityWebRequest(url, "POST");
RequestBody requestBody = new RequestBody();
requestBody.queryInput = new QueryInput();
requestBody.queryInput.audioConfig = new InputAudioConfig();
requestBody.queryInput.audioConfig.audioEncoding = AudioEncoding.AUDIO_ENCODING_UNSPECIFIED;
//TODO: check if that the sample rate hertz
requestBody.queryInput.audioConfig.sampleRateHertz = 16000;
requestBody.queryInput.audioConfig.languageCode = "en";
requestBody.inputAudio = sampleString;
requestBody.detectIntentResponse = new DetectIntentResponse();
requestBody.detectIntentResponse.outputAudioConfig = new OutputAudioConfig();
requestBody.detectIntentResponse.outputAudioConfig.outputAudioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16;
requestBody.detectIntentResponse.outputAudioConfig.sampleHertzRate = 16000;
string jsonRequestBody = JsonUtility.ToJson(requestBody, true);
Debug.Log(jsonRequestBody);
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonRequestBody);
postRequest.SetRequestHeader("Authorization", "Bearer " + AccessToken);
postRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
postRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
//postRequest.SetRequestHeader("Content-Type", "application/json");
yield return postRequest.SendWebRequest();
Debug.Log("Response: " + postRequest.downloadHandler.text);
//text.text = "Response: " + postRequest.downloadHandler.text;
// Or retrieve results as binary data
byte[] resultbyte = postRequest.downloadHandler.data;
string result = System.Text.Encoding.UTF8.GetString(resultbyte);
ResponseBody content = (ResponseBody)JsonUtility.FromJson<ResponseBody>(result);
Debug.Log(content.queryResult.fulfillmentText);
text.text = "Response: " + content.queryResult.fulfillmentText;
byte[] response = content.detectIntentResponse.outputAudio;
一旦我检索到音频,播放音频片段的最佳方法是什么?
所以你要把数据包在一个 AudioClip
,所以可以通过统一的方式来播放。最简单的方法是创建一个 AudioClip
并使用 SetData(float[] data, int offsetSamples)
办法
AudioSource audio = gameObject.AddComponent,AudioSource>() as AudioSource;
audio.clip.SetData(yourAudio, 0);