r/FlutterDev • • 26d ago

Plugin speak: a TTS package for Flutter that doesn't fight your music

I'm building Velorki, an open-source bike navigation app (github.com/orkitec/velorki), and voice prompts with flutter_tts kept causing problems: music not ducking or not coming back, the first syllable cut off on Bluetooth, and no clean way to know when a prompt actually finished. I also wanted to get off CocoaPods. Trunk goes read-only on December 2, and Flutter is moving to Swift Package Manager, but flutter_tts has no SPM support, so it keeps your iOS build on CocoaPods.

So I wrote speak:

final speak = Speak(audio: SpeakAudio.navigation);

await speak('In 200 metres, turn left.');
await speak('Turn left now.', queue: SpeakQueue.interrupt);

Each call tells you how it ended: completed, interrupted, expired, or blocked by a phone call. Audio is only claimed while speaking, so music comes right back, and there's a short lead-in on Bluetooth so nothing gets cut.

No method channels. It talks to AVFoundation on iOS/macOS through FFI, to TextToSpeech on Android through JNI, and to the Web Speech API on the web. The only native code is a small generated Objective-C file, which a build hook compiles, and one Java class on Android (UtteranceProgressListener is abstract, so Dart can't implement it). No CocoaPods, no SPM; just flutter pub add speak. Needs Flutter 3.38+.

It has a thorough test suite running on CI for every supported platform: unit tests, plus device tests against the real speech engines on the iOS simulator, macOS, Chrome and Android emulators. Velorki uses it in production if you want a real-world example.

No Windows or Linux yet. If you need them, let me know: they're doable, I mainly need people who can test on real machines. Same for synthesize-to-file.

It's early (0.2.x), so feedback from real apps is very welcome, especially odd Android engines and car audio.

4 Upvotes

4 comments sorted by

1

u/No_Boss_1911 26d ago

the music not coming back thing is exactly what bit me. i have optional breathing cues that play over whatever the user is listening to and the one rule is their music has to come back to full volume every time, never stuck ducked after a cue. how long is the bluetooth lead in, fixed ms or do you check the route first? and when a call blocks a prompt do you just drop it or can you replay it after

1

u/Electronic-Bug254 26d ago

speak only holds the audio while speaking and gives it back right after (with notifyOthersOnDeactivation on iOS, abandoning focus on Android), so music returns to full volume. A short linger (1.5 s by default) keeps close prompts from making the music pump.

The lead-in is a fixed length (350 ms by default), but only used when it's needed: before the first prompt, speak checks the audio route, and adds the lead-in only if the output is Bluetooth (classic or LE, including hearing aids) or a car, not the phone speaker or wired headphones. Is there another output you'd want it for, e.g. AirPlay or USB audio? Both are configurable:

final speak = Speak(
  audio: SpeakAudio(
    otherAudio: OtherAudio.duck,            // lower music, don't pause it
    linger: Duration.zero,                  // full volume right after each cue
    leadIn: Duration(milliseconds: 500),    // longer for slow headsets
    leadInOnBluetoothOnly: true,            // the default
  ),
);

A blocked prompt is dropped, not replayed (for navigation it's stale by then), but you get SpeakOutcome.blocked back, so you can re-queue it yourself. Would a "call ended" signal help for that?

0

u/[deleted] 26d ago

[removed] — view removed comment

1

u/Electronic-Bug254 26d ago

Thanks! Ducking was one of the reasons that pushed me to write it. What finally worked was claiming the audio only for the duration of each prompt and giving it back right after, so the other app gets its "you can play again" signal every time. Holding the session (or audio focus) for the whole ride is what left music quiet or paused for good.

Out of curiosity, was your project iOS, Android or both? Android audio focus behaves quite differently between OEMs, so I'm collecting cases where it misbehaves.