Skip to content

Commit

Permalink
Fix camera track not stopping on modal close
Browse files Browse the repository at this point in the history
  • Loading branch information
aldrinjenson committed Sep 28, 2022
1 parent 96badf3 commit df03611
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CameraPluginSettings } from './SettingsTab';

class CameraModal extends Modal {
chosenFolderPath: string;
videoStream: MediaStream = null;
constructor(app: App, cameraSettings: CameraPluginSettings) {
super(app);
this.chosenFolderPath = cameraSettings.chosenFolderPath
Expand Down Expand Up @@ -45,7 +46,7 @@ class CameraModal extends Modal {

const chunks: BlobPart[] = [];
let recorder: MediaRecorder = null;
let videoStream: MediaStream = null;
this.videoStream = null;

const cameras = (
await navigator.mediaDevices.enumerateDevices()
Expand All @@ -66,8 +67,8 @@ class CameraModal extends Modal {
}
};

videoStream = await getVideoStream();
if (!videoStream) {
this.videoStream = await getVideoStream();
if (!this.videoStream) {
videoEl.style.display = 'none'
// pText.style.display = 'block'
snapPhotoButton.style.display = 'none'
Expand Down Expand Up @@ -119,16 +120,15 @@ class CameraModal extends Modal {
: `\n![[${filePath}]]\n`,
cursor
);
videoStream && videoStream.getTracks().forEach((track) => {
track.stop();
});
console.log('going to stop');

this.close(); // closing the modal
};


switchCameraButton.onclick = async () => {
cameraIndex = (cameraIndex + 1) % cameras.length;
videoStream = await getVideoStream();
this.videoStream = await getVideoStream();
};

snapPhotoButton.onclick = () => {
Expand All @@ -145,7 +145,7 @@ class CameraModal extends Modal {
}, "image/png");
};

videoEl.srcObject = videoStream;
videoEl.srcObject = this.videoStream;

recordVideoButton.onclick = async () => {
switchCameraButton.disabled = true;
Expand All @@ -158,13 +158,13 @@ class CameraModal extends Modal {
: "Start Recording";

if (!recorder) {
recorder = new MediaRecorder(videoStream, {
recorder = new MediaRecorder(this.videoStream, {
mimeType: "video/webm",
});
}

recorder.ondataavailable = (e) => chunks.push(e.data);
recorder.onstop = async (e) => {
recorder.onstop = async (_) => {
const blob = new Blob(chunks, {
type: "audio/ogg; codecs=opus",
});
Expand All @@ -179,6 +179,9 @@ class CameraModal extends Modal {

onClose() {
const { contentEl } = this;
this.videoStream?.getTracks().forEach(track => {
track.stop()
})
contentEl.empty();
}
}
Expand Down

0 comments on commit df03611

Please sign in to comment.