Skip to content

perf: Use PlayerMode.lowLatency for AudioPools#1963

Merged
Gustl22 merged 9 commits into
bluefireteam:mainfrom
gnarhard:perf/low_latency_mode_for_pools
Feb 2, 2026
Merged

perf: Use PlayerMode.lowLatency for AudioPools#1963
Gustl22 merged 9 commits into
bluefireteam:mainfrom
gnarhard:perf/low_latency_mode_for_pools

Conversation

@gnarhard

Copy link
Copy Markdown
Contributor

Description

Use PlayerMode.lowLatency for AudioPool, a temporary player to get duration of audio on pool creation, and timers to release the player when audio pool player has finished.

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, refactor:,
    docs:, chore:, test:, ci: etc).
  • I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation and added dartdoc comments with ///, where necessary.
  • I have updated/added relevant examples in example.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

Related Issues

@gnarhard gnarhard changed the title Perf: Use PlayerMode.lowLatency for AudioPools perf: Use PlayerMode.lowLatency for AudioPools Dec 29, 2025
@gnarhard

Copy link
Copy Markdown
Contributor Author

Not sure why those tests fail. I was testing this functionality on a physical Android device.

@Gustl22

Gustl22 commented Jan 1, 2026

Copy link
Copy Markdown
Collaborator

@gnarhard thank you for contributing! Personally I think this is more a workaround than a real solution. The issue is not only with pools, but with the player itself, so that we cannot determine the duration in LLM (Low latency mode) for Android.

Initializing a second source just to get the duration destroy all the arguments using LLM in the first place, like less initialization time. One should rather create a tool determining the duration of a source without need to fully initialize / download it (this is probably not scope of this library) e.g. via a bridge thtough ffmpeg or sth.

What I see is the lacking customazibility of the pool, to set the LLM. You may can focus only on that aspect in this PR?
Thank you for the improvement proposal.

…t use LLM on audio pool players. Use FFprobeKit to retrieve the file's duration to avoid creating a temporary player.
@gnarhard

gnarhard commented Jan 1, 2026

Copy link
Copy Markdown
Contributor Author

@Gustl22 My pleasure! I incorporated all of your feedback into the latest commit. Using FFprobeKit is a much more performant method of retrieving the audio file's duration so that was a good call. Since AudioPools are designed to cache many short audio files for rapid playback, I defaulted the usesLowLatencyMode parameter to true. However, in the rare case that someone wants to have a pool of longer audio files, they have that option.

Regarding the failing tests, it looks like ffmpeg_kit_flutter_new wants a minimum deployment target of 14 for iOS and Android wants a minSdkVersion of 24. I'm unfamiliar with how these values should be updated with packages so please let me know how you'd like this updated.

@gnarhard

gnarhard commented Jan 3, 2026

Copy link
Copy Markdown
Contributor Author

@Gustl22 another observation: it looks like ffmpeg forces us to use iOS <=18.6 simulators. That really sucks imo. What is your direction here?

@gnarhard

gnarhard commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

@Gustl22 sorry to blow you up, but I've been doing more testing over here on my iPhone 15 and I'm getting tons of these errors intermittently from getDuration:
PlatformException(SESSION_NOT_FOUND, Session not found., null, null)

I did a quick search of STH, but I'm not sure what you're referring to. Maybe just passing in the duration into create() might be the most reliable solution for now...

@Gustl22

Gustl22 commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

Yes, I did not mean to include ffmpeg in this package, but rather a way to determine the duration and reacting to it from outside of this package. I am not even convinced that one has to stop a low latency player instance immediately, but let it do its thing until a new play event is needed.
If want to make sure it is stopped, I would propose to use the returned Stop function in your application and trigger after a fixed duration (depending on your audio source) or determined via another package.
Low latency mode is only available on the "outdated" package (audioplayers_android), but not on audiplayers_android_exo or all other platforms, which makes finding a reliable solution less attractive and maybe obsolete in a few years. If you want to challenge real low latency on all platforms I advice you to have a look at: https://pub.dev/packages/flutter_soloud

I am happy to include the changes for changing the player mode though.

@gnarhard

gnarhard commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

@Gustl22 I see what you mean now. I'll edit those changes and push it up. Unfortunately with soloud I was unable to play multiple players simultaneously. I'd definitely jump on pulling inspiration from it if my Swift and Java skills were better.

final int maxPlayers;

/// Whether the players in this pool use low latency mode.
final bool usesLowLatencyMode;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be okay to set an playerMode object instead of just setting just the bool? This would give us more flexibility in the future, if we decide to add more options to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely!

Comment thread packages/audioplayers/lib/src/audio_pool.dart Outdated
@Gustl22 Gustl22 enabled auto-merge (squash) February 2, 2026 18:54
@Gustl22 Gustl22 merged commit a97caa1 into bluefireteam:main Feb 2, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants