fix: Android recorderController.stop() never completes
Description
Fixes a hang where await recorderController.stop() on Android never completes, especially for recordings >30 seconds. This was introduced in the 2.0.x rewrite that replaced MediaRecorder with AudioRecord +
MediaCodec.
Root causes fixed:
-
Deadlock in
stopEncoder()—stopEncoder()is called fromonOutputBufferAvailableon the handler thread, but it calledhandlerThread.join(), waiting for itself to finish. Removed thejoin()and usequitSafely()only. -
Race condition in
signalToStop()— EOS buffer queuing now runs on the handler thread viahandler.post {}, avoiding races withonInputBufferAvailablethat caused "MediaCodec discarded an unknown buffer" errors. -
release()called before async completion — For the non-WAV path,release()was called synchronously aftersignalToStop(), before the completion callback fired. Movedrelease()into the completion callback. -
result.success()called from wrong thread —sendRecordingResult()now posts to the main looper, since Flutter'sMethodChannel.Resultmust be called on the platform thread. -
completionCallbackswallowed on error — MovedcompletionCallback?.invoke()to afinallyblock instopEncoder()so the Flutter result is always resolved. -
Missing handler in
setCallback()—mediaCodec.setCallback()now passes thehandlerso callbacks run on the intendedHandlerThread.
Checklist
- [x] The title of my PR starts with a [Conventional Commit] prefix (
fix:,feat:,docs:etc). - [x] I have followed the [Contributor Guide] when preparing my PR.
- [ ] I have updated/added tests for ALL new/updated/fixed functionality.
- [x] I have updated/added relevant documentation in
docsand added dartdoc comments with///. - [x] I have updated/added relevant examples in
examplesordocs.
There's no test infrastructure in the project and the bug is a native Android threading issue that's difficult to unit test from Dart
Breaking Change?
- [ ] Yes, this PR is a breaking change.
- [x] No, this PR is not a breaking change.
Related Issues
#470
Same issue!
_playerControler?.dispose();
This does not work for me it actually crashes the app when I call stop().
Thanks for the fix, @rubenvde! This solves the stop()-never-completing hang nicely.
I've left a few comments on the diff, a couple worth addressing before merge, plus two minor cleanups. Could you take a look and resolve them? Happy to discuss any of them. Thanks again!
Same issue!
_playerControler?.dispose();
Hi @yuanhoujun! Thanks for chiming in. Just a heads-up: this PR is about the recorder- RecorderController.stop() never completing. Your snippet uses PlayerController.dispose(), which is a different subsystem (audio playback), so this fix won't address it.
Could you please check if PR #495 solves your issue? Also worth checking issues #477, #478.
If none of the issues resonate with your bug, could you open a separate issue for the player-dispose problem with a stack trace and repro steps?
@lavigarg-simform We've verified that the core issue this PR addresses is real and matches our production findings. On Galaxy Tab A9 (Android 14), stop() deadlocks 100% for recordings ≥30s — consistent with the self-join deadlock in stopEncoder().
We confirmed through debugging that the hang occurs specifically in the native stop() call, and also in preparePlayer() which shares the same threading model. The 6 threading issues identified here are well-analyzed and the fixes are sound.
Would appreciate this getting merged when the minor feedback is resolved — we currently have to discard recordings on timeout as a workaround.
@lavigarg-simform Are you no longer involved with this library?