Skip to content

Commit

Permalink
Feat (Record): add timeslice option (#3760)
Browse files Browse the repository at this point in the history
- Add mediaRecorderTimeslice option to RecordPluginOptions to which is passed to mediaRecorder.start().
- Add 'record-data-available' event which returns the blob that is delivered by the mediaRecorder and only that blob
  • Loading branch information
nizar authored Jun 21, 2024
1 parent 08afa79 commit 13cc442
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/plugins/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type RecordPluginOptions = {
scrollingWaveform?: boolean
/** The duration of the scrolling waveform window, defaults to 5 seconds */
scrollingWaveformWindow?: number
/** The timeslice to use for the media recorder */
mediaRecorderTimeslice?: number
}

export type RecordPluginDeviceOptions = {
Expand All @@ -30,6 +32,7 @@ export type RecordPluginEvents = BasePluginEvents & {
'record-end': [blob: Blob]
/** Fires continuously while recording */
'record-progress': [duration: number]
'record-data-available': [blob: Blob]
}

type MicStream = {
Expand Down Expand Up @@ -62,6 +65,7 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
scrollingWaveform: options.scrollingWaveform ?? false,
scrollingWaveformWindow: options.scrollingWaveformWindow ?? DEFAULT_SCROLLING_WAVEFORM_WINDOW,
renderRecordedAudio: options.renderRecordedAudio ?? true,
mediaRecorderTimeslice: options.mediaRecorderTimeslice ?? undefined,
})

this.timer = new Timer()
Expand Down Expand Up @@ -193,6 +197,7 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
if (event.data.size > 0) {
recordedChunks.push(event.data)
}
this.emit('record-data-available', event.data)
}

const emitWithBlob = (ev: 'record-pause' | 'record-end') => {
Expand All @@ -208,7 +213,7 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {

mediaRecorder.onstop = () => emitWithBlob('record-end')

mediaRecorder.start()
mediaRecorder.start(this.options.mediaRecorderTimeslice)
this.lastStartTime = performance.now()
this.lastDuration = 0
this.duration = 0
Expand Down

0 comments on commit 13cc442

Please sign in to comment.