Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly close the video stream in getCamera #42

Merged
merged 2 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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