Skip to content

fix: remove MediaRecorder audio data listener on stop - #478

Open
RaphaelFakhri wants to merge 1 commit into
livekit:mainfrom
RaphaelFakhri:fix/media-recorder-duplicate-chunks
Open

RaphaelFakhri wants to merge 1 commit into
livekit:mainfrom
RaphaelFakhri:fix/media-recorder-duplicate-chunks

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes #477

MediaRecorder.stop() deletes the native audio sink but leaves the JS LK_AUDIO_DATA subscription that start() added in place. Each start() adds a new subscription, and every subscription checks the event against the current _reactTag. After a recorder is stopped and started again, all of its subscriptions match the new sink, so every audio chunk is stored once per previous start(). The second recording comes back with each chunk doubled, the third tripled, and so on. The subscriptions also keep the recorder alive after it is no longer used.

unregisterListener() now calls removeListener(this) and clears _reactTag after it deletes the native sink. Clearing the tag also stops a second stop() call from deleting the same native sink again.

src/__tests__/MediaRecorder.test.ts mocks the native module, sends LK_AUDIO_DATA events through DeviceEventEmitter, and checks that:

  • A recorder that is started, stopped, and started again returns each chunk once. On main, the second recording returns [4, 5, 6, 4, 5, 6] instead of [4, 5, 6].
  • Calling stop() twice deletes the native sink once. On main, it is deleted twice.

Both cases fail on main and pass with this change. yarn test, yarn lint (no new warnings), and yarn typescript pass.

Changeset included.

@changeset-bot

changeset-bot Bot commented Oct 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 19bbcd9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@livekit/react-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

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.

MediaRecorder duplicates audio data after stop() and start()

1 participant