Mohit Singh
Mohit Singh

Reputation: 367

Send chunked Audio request to wit.ai from dart

I'm looking to use wit.ai for speech recognition. This is the post method I'm using to post audio file.

  Future<http.Response> post(
    String path,
    File file,
  ) async {
    final client = http.Client();

    try {
      final uri = Uri.parse('https://api.wit.ai/$path');
      final len = file.lengthSync();

      final request = http.StreamedRequest('POST', uri)
        ..headers.addAll({
          'Authorization': 'Bearer $api_key',
          'Content-Type': 'audio/wav',
          'Transfer-encoding': 'chunked'
        })
        ..contentLength = len;

      file.openRead().listen((chunk) {
        request.sink.add(chunk);
      }, onDone: () {
        print('done');
        request.sink.close();
      });

      final res = await request.send().then(http.Response.fromStream);

      return res;
    } catch (e) {
      rethrow;
    } finally {
      client.close();
    }
  }

Wit.ai seems to limit audio length to 20 seconds. So, I tried to send a chunked request. I've never worked with chunked requests before and I expected it to send smaller chunks and get stream of response for each chunk seperately. But it seems that the request is sent once only and I'm still getting audio length exceeded error. Am I doing something wrong and Is it possible to get text for audio of length more than 20 seconds from wit speech api?

Upvotes: 2

Views: 333

Answers (0)

Related Questions