How to Stop Audio Playback from OpenAI TTS (Custom Action with AudioPlayer)

Custom Code

Hi all, we’re running into a persistent issue and would love help from the community or FlutterFlow team:

We’re using a custom action that:

  1. Sends a text string to OpenAI’s /v1/audio/speech endpoint for TTS.

  2. Receives MP3 bytes and saves to a local file.

  3. Plays the file using AudioPlayer(playerId: roleID) with DeviceFileSource.

Playback works beautifully. The problem is:
👉 We cannot reliably stop the audio using another custom action.

Here’s what we tried:

  • Creating a separate stopSpeaking(playerId) action that re-creates the same AudioPlayer(playerId: roleID) and calls await player.stop().

  • We store the playerId in a shared FFAppState().currentPlayerId so we’re referencing the same ID.

  • However, the stop() call often has no effect — the audio keeps playing.

We believe this is either:

  • A scoping issue (new AudioPlayer() with same playerId isn't actually sharing the same instance), or

  • A FlutterFlow runtime limitation where each custom action has its own isolated execution context, breaking shared memory.

  • I tried to create a singleton using custom code file, but custom actions can not leverage it and compiler always threw errors. If any one know how to may that work, please let me know.


Question:
🟡 Is there a supported way in FlutterFlow custom actions to stop a playing AudioPlayer instance from a separate action?

🟡 Does AudioPlayer(playerId: roleID) guarantee instance sharing across actions?

🟡 Is there a better FF-supported pattern (global object, singleton, or workaround) for persistent audio control?

We’ve already tried:

  • Defining a singleton TTSPlayerSingleton.player — works locally but can’t be accessed across actions in FF.

  • Using custom functions (but they don’t support side effects like audio control).

Any help or insight would be appreciated!

What have you tried so far?
  • Defining a singleton TTSPlayerSingleton.player — works locally but can’t be accessed across actions in FF.

  • Using playerId referenced by both TTS action and stopSpeaking action, but does not stop it although have the same playerId.

  • FF out of box Play Sound and Stop do not support local MP3 file.

Did you check FlutterFlow's Documentation for this topic?
No
4
1 reply