openai_tts
A lightweight Flutter package that integrates with OpenAI's Text-to-Speech API. Supports both real-time streaming (via PCM) and downloadable audio (MP3), powered by OpenAI's latest voice models.
ποΈ Generate lifelike speech with minimal latency and stream it directly in your app.
Features
- π Text-to-speech using OpenAIβs
tts-1andtts-1-hdmodels - π΅ Supports both
mp3and real-timepcmaudio streaming - π Built-in streaming playback via
flutter_sound - π Control over playback (pause, resume, stop)
- π Supports multiple realistic voices
Installation
Add the following to your pubspec.yaml:
dependencies:
openai_tts: ^<latest_version>
Then run:
flutter pub get
Getting Started
Import the package:
import 'package:openai_tts/openai_tts.dart';
Create an instance:
final tts = OpenaiTTS(apiKey: 'sk-...'); // Use your OpenAI API key
1. Download and use MP3:
final audioData = await tts.createSpeak("Hello, world!");
// Save or play using your preferred audio player
2. Stream audio in real time:
await tts.streamSpeak("Streaming this sentence in real-time.");
3. Control playback:
await tts.pausePlayer();
await tts.resumePlayer();
await tts.stopPlayer();
Configuration
Change Voice:
tts.setVoice = OpenaiTTSVoice.shimmer;
Available voices:
- alloy
- echo
- fable
- onyx
- nova
- shimmer
- ash
- ballad
- coral
- verse
- sage
Change Model:
tts.setModel = OpenaiTTSModel.tts1hd;
Models:
tts-1tts-1-hd
Requirements
- Flutter 3.10 or newer
- Valid OpenAI API key with TTS access
- Internet connection
Example
Full example:
void main() async {
final tts = OpenaiTTS(apiKey: 'sk-your-api-key');
tts.setVoice = OpenaiTTSVoice.nova;
tts.setModel = OpenaiTTSModel.tts1;
await tts.streamSpeak("Welcome to OpenAI TTS with Flutter.");
}
Troubleshooting
- Make sure your OpenAI API key is valid and has TTS access.
- Streaming works best on physical devices with low audio latency.
- Use the
createSpeakmethod if you experience issues with real-time playback.
License
MIT License Β© 2025 Your Name or Organization