textToSpeech method

Future<Uint8List> textToSpeech(
  1. String text,
  2. String language,
  3. String? voice
)

Implementation

Future<Uint8List> textToSpeech(String text, String language,String? voice) async {

   final Map<String, dynamic> body = {
    'language': language,
    'text': text,
  };

  if (voice != null) {
    body['voice'] = voice;
  }

  final response = await http.post(
    Uri.parse(baseUrl),
    headers: {'Content-Type': 'application/json'},
    body: jsonEncode(body),
  );

  if (response.statusCode == 200) {
    return response.bodyBytes;  // This returns the audio data as bytes.
  } else {
    throw Exception('Failed to convert text to speech');
  }
}