From f1df664f06dfd3b1874e075cdfc1356ecbe4cd47 Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Tue, 8 Aug 2023 15:12:43 +0530 Subject: [PATCH] skip sending data if content is paused --- Audio-Transcription-Firefox/content.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Audio-Transcription-Firefox/content.js b/Audio-Transcription-Firefox/content.js index d849b0d..53f216f 100644 --- a/Audio-Transcription-Firefox/content.js +++ b/Audio-Transcription-Firefox/content.js @@ -5,6 +5,20 @@ let audioContext = null; let scriptProcessor = null; let language = null; +let isPaused = false; + +const mediaElements = document.querySelectorAll('video, audio'); +mediaElements.forEach((mediaElement) => { + mediaElement.addEventListener('play', handlePlaybackStateChange); + mediaElement.addEventListener('pause', handlePlaybackStateChange); +}); + + +function handlePlaybackStateChange(event) { + isPaused = event.target.paused; +} + + /** * Resamples the audio data to a target sample rate of 16kHz. * @param {Array|ArrayBuffer|TypedArray} audioData - The input audio data. @@ -90,7 +104,7 @@ function startRecording(data) { recorder = audioContext.createScriptProcessor(4096, 1, 1); recorder.onaudioprocess = async (event) => { - if (!audioContext || !isCapturing || !isServerReady) return; + if (!audioContext || !isCapturing || !isServerReady || isPaused) return; const inputData = event.inputBuffer.getChannelData(0); const audioData16kHz = resampleTo16kHZ(inputData, audioContext.sampleRate);