]> git.taranathan.com Git - audio-stream.git/commitdiff
screen recording main
authorTaran Nathan <moogoesmeow123@gmail.com>
Tue, 21 Jul 2026 03:57:43 +0000 (20:57 -0700)
committerTaran Nathan <moogoesmeow123@gmail.com>
Tue, 21 Jul 2026 09:19:43 +0000 (02:19 -0700)
script.js

index 553481880a0c92a3bc25415c36e1eef5c30aef5c..ec6545378012c652069ead203abc68294512c1d6 100644 (file)
--- a/script.js
+++ b/script.js
@@ -1,16 +1,63 @@
 let mediaRecorder;
 let audioChunks = [];
 let recordedAudioUrl = "";
 let mediaRecorder;
 let audioChunks = [];
 let recordedAudioUrl = "";
+let microphoneStream;
+let systemStream;
+let mixedAudioStream;
+let audioContext;
+let mixedDestination;
 
 const startButton = document.getElementById("btnStart");
 const stopButton = document.getElementById("btnStop");
 const playButton = document.getElementById("btnPlay");
 const audioElement = document.getElementById("audioPlay");
 
 
 const startButton = document.getElementById("btnStart");
 const stopButton = document.getElementById("btnStop");
 const playButton = document.getElementById("btnPlay");
 const audioElement = document.getElementById("audioPlay");
 
+function cleanupCaptureResources() {
+  [microphoneStream, systemStream, mixedAudioStream].forEach((stream) => {
+    if (stream) {
+      stream.getTracks().forEach((track) => track.stop());
+    }
+  });
+
+  if (audioContext) {
+    audioContext.close();
+  }
+
+  microphoneStream = null;
+  systemStream = null;
+  mixedAudioStream = null;
+  audioContext = null;
+  mixedDestination = null;
+}
+
 startButton.addEventListener("click", async () => {
   try {
 startButton.addEventListener("click", async () => {
   try {
-    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
-    mediaRecorder = new MediaRecorder(stream);
+    microphoneStream = await navigator.mediaDevices.getUserMedia({ audio: true });
+    systemStream = await navigator.mediaDevices.getDisplayMedia({
+      video: true,
+      audio: true
+    });
+
+    console.log(systemStream.getTracks());
+
+    if (systemStream.getAudioTracks().length === 0) {
+      console.error( "No screen share audio track. Microphone only." );
+      systemStream.getTracks().forEach((track) => track.stop());
+      systemStream = null;
+    }
+
+    audioContext = new AudioContext();
+    mixedDestination = audioContext.createMediaStreamDestination();
+
+    const micSource = audioContext.createMediaStreamSource(microphoneStream);
+    micSource.connect(mixedDestination);
+    if (systemStream) {
+      const systemSource = audioContext.createMediaStreamSource(systemStream);
+      systemSource.connect(mixedDestination);
+    }
+
+    mixedAudioStream = mixedDestination.stream;
+    mediaRecorder = new MediaRecorder(mixedAudioStream);
     audioChunks = [];
 
     mediaRecorder.addEventListener("dataavailable", (event) => {
     audioChunks = [];
 
     mediaRecorder.addEventListener("dataavailable", (event) => {
@@ -32,7 +79,7 @@ startButton.addEventListener("click", async () => {
       audioElement.src = recordedAudioUrl;
       playButton.disabled = false;
 
       audioElement.src = recordedAudioUrl;
       playButton.disabled = false;
 
-      stream.getTracks().forEach((track) => track.stop());
+      cleanupCaptureResources();
     });
 
     mediaRecorder.start();
     });
 
     mediaRecorder.start();
@@ -40,7 +87,11 @@ startButton.addEventListener("click", async () => {
     stopButton.disabled = false;
     playButton.disabled = true;
   } catch (error) {
     stopButton.disabled = false;
     playButton.disabled = true;
   } catch (error) {
-    console.error("Microphone access failed:", error);
+    cleanupCaptureResources();
+    startButton.disabled = false;
+    stopButton.disabled = true;
+    playButton.disabled = !recordedAudioUrl;
+    console.error("Audio capture setup failed:", error);
   }
 });
 
   }
 });