I am building something and i need it to use openai tts in my flutter app
Everything is good but the audio response can take some time sometimes.
So it would be nice if i can stream it but coundnt find any documentation to stream with api.
Please help if you have any idea
https://platform.openai.com/docs/guides/text-to-speech/streaming-real-time-audio
You can just start playing the audio file as soon as some of the chunks have been downloaded.
instead of using file i am using direct bits to play it without it needing to download to a file
Future<Uint8List> generateAudioFromText(String text, String voice) async {
final url = Uri.parse(AppConstants.openAISpeech);
final headers = {
'Authorization': 'Bearer ${AppConstants.openAIApiKey}',
'Content-Type': 'application/json',
};
final body = jsonEncode({
'model': 'tts-1',
'input': text,
'voice': voice,
});
final response = await http.post(url, headers: headers, body: body);
if (response.statusCode == 200) {
return response.bodyBytes;
} else {
throw Exception('Failed to generate audio: ${response.body}');
}
}
That should work too.
Quick question, doesn’t this expose your OpenAI API key? How do you make sure it’s safe without using any backed/middleware service?