Skip to content

Android : await recorderController.stop(); sometimes never completes #470

Description

@Tom3652

Describe the bug
Since i have upgraded to 2.0.1 version, i have noticed (reproductible) that for audio recordings, when i call String? path = await recorderController.stop(); i don't see the log that is right after and the wave is not generated like frozen.
This only happens on Android, iOS works fine with the same code.

To Reproduce
Steps to reproduce the behavior:

  1. Call .record on Android (physical device)
  2. Record an audio for at least 30sec - 1min
  3. Call .stop as above
  4. Print / log the path output
  5. See that this log is never shown

Expected behavior
The method await recorderController.stop(); should complete like on version < 2.0.x on Android

Smartphone (please complete the following information):

  • Device: Motorola
  • OS: Android
  • Version : 11

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
This is reproductible no matter the size of the recording, but i think it's easier to reproduce it when the recording is large > 30 seconds.
I have customers on my live that app that have reported they have this issue with recordings less than 5sec but not all the time. However, more than 30 seconds it's almost 100% that the .stop doesn't complete.

Note : this is not device-specific since i could reproduce it with my Motorola, but my customers have Google Pixel 7, Samsung A51 for example with various Android version (11 - 14 - 15)

Activity

  1. Tom3652 commented on Jan 7, 2026

    @Tom3652
    Author

    Here is how we record maybe this has an impact :

        recorderController?.reset();
        await recorderController?.record(
              path: outputPath,
              recorderSettings: const RecorderSettings(
                  androidEncoderSettings:
                      AndroidEncoderSettings(androidEncoder: AndroidEncoder.aacLc),
                  bitRate: 48000,
                  sampleRate: 44100));

    On Android.

  2. ujas-m-simformsolutions commented on Jan 8, 2026

    @ujas-m-simformsolutions
    Contributor

    Thanks for reporting this issue. We can reproduce this issue.

  3. added
    bugSomething isn't working
    and removed
    in triageThe issue is being triaged
    on Jan 8, 2026
  4. mostafasany commented on Jan 18, 2026

    @mostafasany

    Hello, Do you have expected resolution date?

  5. GereyTatarkhanov commented on Jan 19, 2026

    @GereyTatarkhanov

    I have same problem

  6. AmrTawfek commented on Jan 22, 2026

    @AmrTawfek

    Same Issue

  7. mzubairr999 commented on Jan 22, 2026

    @mzubairr999

    @ujas-m-simformsolutions kindly fix this issue it effecting much
    even recorder is paused and call stop():
    it never return path it stuck

  8. hunghuy201280 commented on Feb 3, 2026

    @hunghuy201280

    is there any workarounds?

  9. Tom3652 commented on Feb 15, 2026

    @Tom3652
    Author

    i have not found any yet...

  10. Abaxx commented on Mar 8, 2026

    @Abaxx

    I am having same issue.
    Any workaround yet?

  11. DorraY commented on Apr 9, 2026

    @DorraY

    Same issue on Galaxy

  12. lavigarg-simform commented on Jun 23, 2026

    @lavigarg-simform

    Hi @DorraY! Thanks for testing PR #486 and reporting that you still hit a crash (reference).

    To help me track it down, could you share-

    • The full stack trace / logcat from the crash
    • Steps to reproduce (recording length, whether the recorder was paused before stop, etc.)
    • The code snippet you used (your record(...) settings and the stop() call)

    That would help a lot in reproducing it on my end. Thanks!

  13. DorraY commented on Jun 23, 2026

    @DorraY

    i did this a long time ago
    but it was on Samsung Galaxy A54 5g
    a record duration of less than 30 seconds

  14. lavigarg-simform commented on Jun 24, 2026

    @lavigarg-simform

    i did this a long time ago but it was on Samsung Galaxy A54 5g a record duration of less than 30 seconds

    @DorraY Without a stack trace, it's hard to tell if something beyond what's being addressed in the PR is causing this on your end. Could you please test once the PR gets updated by its author and let us know if it resolves the issue? Your confirmation would be really helpful!

  15. iawaisrana commented on Aug 4, 2026

    @iawaisrana

    Hit this in production too — here's what's happening in plain terms:

    When you call stop(), the plugin tells the audio encoder "wrap up." Normally the encoder replies "done" right away, and #486 fixes a bug where that reply could get stuck. But there's a second problem: sometimes the encoder never gets a chance to reply at all, because recording already stopped feeding it new audio. When that happens, stop() just waits forever for a reply that's never coming.

    #508 fixes both: it includes #486's fix, plus a safety timer — if the encoder doesn't reply within a short window (default 500ms, adjustable), the plugin stops waiting and finishes the stop itself.

    Workaround until it's merged — point your audio_waveforms dependency at the fix branch:

    dependencies:
      audio_waveforms:
        git:
          url: https://github.com/iawaisrana/audio_waveforms.git
          ref: fix/android-stop-deadlock

    If you want to change how long the safety timer waits, set stopTimeoutMs on AndroidEncoderSettings:

    RecorderSettings(
      androidEncoderSettings: AndroidEncoderSettings(
        stopTimeoutMs: 1000, // milliseconds, default is 500
      ),
    )
  16. victorrss commented on Aug 21, 2026

    @victorrss

    We hit this independently and traced it to a cause that I don't think has been named yet in this thread, and that neither #486 nor #508 closes: CommonEncoder leaks input buffers, so the codec's input pool drains over the course of a recording.

    onInputBufferAvailable stores the index in a single field:

    private var currentInputBufferIndex = -1
    ...
    override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
        ...
        currentInputBufferIndex = index
    }

    In async mode MediaCodec can hand you several input buffers before you consume any of them. Each callback overwrites the previous index, and every overwritten index is lost — that buffer is never queued back, so it never returns to the pool. The longer the recording, the more of the pool is gone, which lines up with the reports here that >30s reproduces almost every time while short clips only fail sometimes.

    The endgame is exactly the symptom @iawaisrana described: by the time signalToStop() runs, currentInputBufferIndex is -1 and no further onInputBufferAvailable is coming (audioRecord.stop() has already run), so EOS is never queued, the muxer never finalizes, and stop() never returns. On the AAC/MPEG-4 path the output file is also left without its moov atom, so even the partial recording is unplayable.

    What worked for us was fixing the accounting rather than bounding the wait:

    • keep every available index in a queue (LinkedList<Int>) instead of one field, and drain it in feedEncoder while there is pending input;
    • queue EOS as soon as it is safe to — checked from both signalToStop() and onInputBufferAvailable, guarded by a flag so it is queued exactly once — instead of only from the next callback;
    • create a fresh CommonEncoder per recording, so a session that did hang cannot poison the next one.

    Since the fix removes the starvation, we haven't needed a grace-period fallback like the one in #508 — though a bounded fallback is still a reasonable belt-and-braces measure for OEM oddities, and it composes fine with this.

    Sharing the analysis rather than a competing PR, since #486 and #508 are already in flight here and the maintainers are best placed to decide the shape. Happy to open one if it would help.

  17. iawaisrana commented on Sep 30, 2026

    @iawaisrana

    Thanks @victorrss, that analysis was spot on. The single currentInputBufferIndex field was dropping buffers, and #508's grace period was only covering for it: stop() would return, but the recording could be cut short wherever encoding had stalled.

    #508 is now updated with a fix for the root cause, along the lines you suggested:

    • every available input buffer index is kept in a queue and drained together with the pending audio
    • EOS is queued exactly once, as soon as the backlog drains, rather than waiting for a callback that may never come
    • a fresh CommonEncoder is created per recording
    • stop() also joins the recording thread before signalling EOS, so no audio is fed after it

    The stopTimeoutMs fallback stays as a belt-and-braces measure for OEM oddities. With the buffer accounting fixed, it shouldn't fire in normal use.

    Verified on a physical device with 30s+ recordings, back-to-back recordings, pause then stop, and short clips. stop() returned every time and the files played through to the end.

    If you're already on the fix branch, run flutter pub upgrade audio_waveforms to pick up the new commit:

    dependencies:
      audio_waveforms:
        git:
          url: https://github.com/iawaisrana/audio_waveforms.git
          ref: fix/android-stop-deadlock

    If you still hit a hang, logcat with the tag audio_waveforms would help. In particular, check whether Encoder did not finish within the grace period shows up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions