Skip to content

Commit

Permalink
Merge pull request #42 from mebjas/getcamera-videofix
Browse files Browse the repository at this point in the history
Explicitly close the video stream in getCamera
  • Loading branch information
mebjas authored May 9, 2020
2 parents d4add7a + f60e8c5 commit 9d1f174
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
15 changes: 14 additions & 1 deletion html5-qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,19 @@ class Html5Qrcode {
&& navigator.mediaDevices.getUserMedia) {
this._log("navigator.mediaDevices used");
navigator.mediaDevices.getUserMedia({ audio: false, video: true })
.then(_ => {
.then(stream => {
// hacky approach to close any active stream if they are active.
stream.oninactive = _ => this._log("All streams closed");
const closeActiveStreams = stream => {
const tracks = stream.getVideoTracks();
for (var i = 0; i < tracks.length; i++) {
const track = tracks[i];
track.enabled = false;
track.stop();
stream.removeTrack(track);
}
}

navigator.mediaDevices.enumerateDevices()
.then(devices => {
const results = [];
Expand All @@ -527,6 +539,7 @@ class Html5Qrcode {
}
}
this._log(`${results.length} results found`);
closeActiveStreams(stream);
resolve(results);
})
.catch(err => {
Expand Down
Loading

0 comments on commit 9d1f174

Please sign in to comment.