Skip to content

fix: 🐛 Reset paused state when starting a new recording on iOS - #521

Open
yash-fadadu-simform wants to merge 1 commit into
mainfrom
fix/reset_paused_state_on_record
Open

yash-fadadu-simform wants to merge 1 commit into
mainfrom
fix/reset_paused_state_on_record

Conversation

@yash-fadadu-simform

@yash-fadadu-simform yash-fadadu-simform commented Oct 27, 2026 •

Copy link
Copy Markdown

Description

Fixes #527

On iOS, stopping a recording while it is paused breaks the next recording. record() succeeds and the state is recording, but onAudioChunks never fires and the live waveform stays flat. The recording after that works again, so the failures alternate: works, flat, works, flat.

Cause: RecorderBytesStreamEngine keeps a paused flag that pause() and resume() toggle. stop() (detach()) never reset it. Stopping while paused left the flag true, so the next recording's input tap dropped every buffer. Pausing that silent recording toggled the flag back to false, which is why the one after it worked.

Fix: attach() now sets paused = false before installing the tap, so every new recording starts unpaused.

macOS is not affected: its recorder sets isPaused explicitly and clears it on start and stop.

Before / After

Before and after: record → stop → record and record → pause → stop → record

Repro harness in the example app on the iPhone 13 simulator (iOS 18.1), 16 kHz AAC, overrideAudioSession: true. Each cycle records for 1.5 s and counts onAudioChunks events; a cycle fails when none arrive.

Recordings that got audio, all three builds run back to back:

Scenario main New AVAudioEngine per recording, no paused reset This PR
record → stop → record 10/10 10/10 10/10
record → pause → stop → record 3/6 (2nd, 4th, 6th silent) 3/6 (2nd, 4th, 6th silent) 6/6

The screenshot's AFTER panel was captured on an earlier build that also created a new AVAudioEngine per recording. The middle column shows that change alone leaves the bug in place; the paused reset in this PR is what fixes it.

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs: etc).
  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

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

@yash-fadadu-simform
yash-fadadu-simform force-pushed the fix/reset_paused_state_on_record branch from 0b253e1 to e2b3e94 Compare October 27, 2026 07:56

@vasu-nageshri-simform vasu-nageshri-simform left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve. The fix is correct and minimal. No blocking issues.

Comments on lines outside the diff:

[Nit] ios/Classes/RecorderBytesStreamEngine.swift:53 (detach()): also reset paused = false in detach(), so stop leaves the engine clean and attach() doesn't depend on it. Keeping the reset in attach() as well is fine.

[Nit] ios/Classes/RecorderBytesStreamEngine.swift:15: paused is written on the main thread and read on the audio render thread without synchronization. It works in practice for a Bool, but if the setter refactor below happens, consider an atomic or a serial queue.

Heads-up: this will need a rebase onto the new ios/audio_waveforms/Sources/audio_waveforms/ paths if the Swift Package Manager PR lands first.

@yash-fadadu-simform
yash-fadadu-simform force-pushed the fix/reset_paused_state_on_record branch from 50b65b7 to bdf6a83 Compare October 30, 2026 04:39
@yash-fadadu-simform

Copy link
Copy Markdown
Author

Addressed the review-summary nits:

  • detach() now also resets paused = false. The reset in attach() is kept too.
  • paused is now guarded by an NSLock, since it is written on the main thread and read on the render thread.

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.

iOS: onAudioChunks and live waveform get no data when recording again after stopping a paused recording

2 participants