我一直在尝试实现 Microsoft 认知语音转文本 API 将音频文件转换为文本,但我总是收到错误请求消息。
这是我正在实现的示例代码:
public static object MC()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", speechKey);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("audio/wav"));
var mp = new MultipartFormDataContent();
var file = new StreamContent(File.OpenRead(HttpContext.Current.Server.MapPath("~/audios/audio_06012023094443961.wav")));
file.Headers.ContentType = new MediaTypeHeaderValue("audio/wav");
mp.Add(file, "audio", "audio_06012023094319156.wav");
var resp = client.PostAsync("https://" + speechRegion + ".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US", mp);
while (!resp.IsCompleted)
{
Thread.Sleep(100);
}
return resp.Result.ReasonPhrase;
}
我的编程语言是C#。
确保包含“User-Agent”标头。由于某种未知的原因,Microsoft 认知语音 API 强制请求拥有一个。