Fix ~230 ms Flutter UI freeze when Android recording starts - #620
Open
AlexDochioiu wants to merge 1 commit into
Open
Fix ~230 ms Flutter UI freeze when Android recording starts#620AlexDochioiu wants to merge 1 commit into
AlexDochioiu wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
On Android,
AudioRecorder.start()ultimately waits forRecordThreadinitialization through aCountDownLatch.MethodCallHandlerImplcurrently invokes that work synchronously, so starting a recording blocks Flutter's Android platform/UI thread whileAudioRecordinitializes.This freezes visible Flutter UI feedback for the duration of recorder startup. In an interface that animates immediately when the user presses a record control, the animation does not merely start late: its frames stop rendering until native initialization returns.
Repeated measurements on a Pixel 10 Pro running Android 17 showed:
The impact is not specific to one animation or application. Any Flutter UI expected to remain responsive while Android recording starts can visibly freeze because the plugin performs blocking native work on Flutter's platform thread.
What
Complexity
Moderate. Moving the blocking call is small, but preserving recorder ordering, Bluetooth continuations, Flutter channel thread affinity, and engine-detach cleanup crosses several Android lifecycle boundaries.
Risk and test focus
The main risks are call reordering, callbacks escaping back onto the UI thread, duplicate recorder IDs, and disposal racing startup. The implementation uses one serial queue per recorder, rejects calls once disposal starts, keeps recorder IDs reserved through channel teardown, and waits for queued cleanup during engine detach.
There is no Dart API, wire-contract, persisted-data, permission, or database impact.
Expected result
Starting an Android recording no longer freezes Flutter UI feedback while native recorder initialization completes. Recorder method ordering and existing results remain intact, including with multiple recorder instances and lifecycle teardown.
Verification
flutter analyze record_androidflutter build apk --debugfromrecord/example./gradlew :record_android:testDebugUnitTestfromrecord/example/android