尝试使用 Azure 语音识别和 Flutter 时出现错误 400

Getting error 400 when trying to use Azure Speech Recognition and Flutter

我的任务是在 Flutter 应用程序上使用 Azure 语音识别 API。该应用程序应该记录用户的语音并将其发送到 Azure API。我尝试使用我能找到的唯一 pub.dev 插件,但它没有用,而且文档中没有 Flutter 示例。由于请求在 Postman 上返回 200 并且我能够使其在 Javascript 应用程序上运行,问题一定是我的 Flutter 应用程序,可能是请求中的某些内容,因为它返回代码 400(错误请求),说该请求包含无效数据。

下面的代码是我对API的请求。我用来获取字节的文件是一个包含录制语音的 wav 文件

你能帮帮我吗?感谢关注

      var bytes = file.readAsBytesSync();
      var response = await Dio().post(
        "https://brazilsouth.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=pt-BR",
        data: bytes,
        options: Options(
            headers: {
              "Ocp-Apim-Subscription-Key": "subscriptionKey",
              "Content-Type": "audio/wav"
            },
      );
      print(response.statusCode);

解决这个问题几天了,终于回复成功了!

Future<dynamic> speechToText(File file) async {
    
    final bytes = file.readAsBytesSync();

    var headers = {
      'Ocp-Apim-Subscription-Key': key,
      'Content-Type': 'audio/wav'
    };

    var response;
    Map<String, dynamic> responseBody;
    var recognizedVoiceText;

    try {
      response = await http.post(
        "https://brazilsouth.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=pt-BR",
        body: bytes,
        headers: headers,
      );

      // The response body is a string that needs to be decoded as a json in order to extract the text.
      responseBody = jsonDecode(response.body);
      recognizedVoiceText = responseBody["DisplayText"];
    } catch (e) {
      print('Error: ${e.toString()}');
      recognizedVoiceText = "Something went wrong";
    }

    return recognizedVoiceText;
  }