Skip to content

Commit

Permalink
fix: Pausing for too long with Chromium based browsers breaks the rec…
Browse files Browse the repository at this point in the history
…ording.

closes #316
  • Loading branch information
llfbandit committed May 22, 2024
1 parent 1546932 commit 92ab18f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions record_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.1.1
* fix: Pausing for too long with Chromium based browsers breaks the recording.

## 1.1.0
* feat: Migrated implementation to package:web.

Expand Down
25 changes: 19 additions & 6 deletions record_web/lib/recorder/delegate/mic_recorder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class MicRecorderDelegate extends RecorderDelegate {
// Media stream get from getUserMedia
web.MediaStream? _mediaStream;
web.AudioContext? _context;
web.AudioWorkletNode? _workletNode;
web.MediaStreamAudioSourceNode? _source;

StreamController<Uint8List>? _recordStreamCtrl;
Encoder? _encoder;
// Amplitude
Expand Down Expand Up @@ -49,17 +52,25 @@ class MicRecorderDelegate extends RecorderDelegate {
Future<void> pause() async {
final context = _context;
if (context != null && context.state == 'running') {
onStateChanged(RecordState.pause);
await context.suspend().toDart;
onStateChanged(RecordState.pause);
}
}

@override
Future<void> resume() async {
final context = _context;
if (context != null && context.state == 'suspended') {
onStateChanged(RecordState.record);
await context.resume().toDart;

if (_workletNode != null) {
// Workaround for Chromium based browsers,
// Audio worklet node is disconnected
// when pause state is too long (> 12~15 secs)
_source?.connect(_workletNode!)?.connect(context.destination);
}

onStateChanged(RecordState.record);
}
}

Expand Down Expand Up @@ -118,7 +129,7 @@ class MicRecorderDelegate extends RecorderDelegate {
.addModule('./assets/packages/record_web/assets/js/record.worklet.js')
.toDart;

final recorder = web.AudioWorkletNode(
final workletNode = web.AudioWorkletNode(
context,
'recorder.worklet',
web.AudioWorkletNodeOptions(
Expand All @@ -129,7 +140,7 @@ class MicRecorderDelegate extends RecorderDelegate {
),
);

source.connect(recorder)?.connect(context.destination);
source.connect(workletNode)?.connect(context.destination);

if (!isStream) {
_encoder?.cleanup();
Expand All @@ -145,13 +156,15 @@ class MicRecorderDelegate extends RecorderDelegate {
}

if (isStream) {
recorder.port.onmessage =
workletNode.port.onmessage =
((web.MessageEvent event) => _onMessageStream(event)).toJS;
} else {
recorder.port.onmessage =
workletNode.port.onmessage =
((web.MessageEvent event) => _onMessage(event)).toJS;
}

_source = source;
_workletNode = workletNode;
_context = context;
_mediaStream = mediaStream;

Expand Down
2 changes: 1 addition & 1 deletion record_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: record_web
description: Web specific implementation for record package called by record_platform_interface.
version: 1.1.0
version: 1.1.1
homepage: https://github.com/llfbandit/record/tree/master/record_web

environment:
Expand Down

0 comments on commit 92ab18f

Please sign in to comment.