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

Stop stream even when then component is unloaded before preview is shown #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ module.exports = class Reader extends Component {
this.clearComponent()
}
clearComponent() {
// Flag stream to stop
this.shouldStop = true
// Remove all event listeners and variables
if (this.timeout) {
clearTimeout(this.timeout)
Expand All @@ -143,8 +145,9 @@ module.exports = class Reader extends Component {
const isFirefox = /firefox/i.test(navigator.userAgent);
const isSafari = !!navigator.userAgent.match(/Version\/[\d/]+.*Safari/);

let supported = {}
if (navigator.mediaDevices && typeof navigator.mediaDevices.getSupportedConstraints === 'function') {
const supported = navigator.mediaDevices.getSupportedConstraints()
supported = navigator.mediaDevices.getSupportedConstraints()
}
const constraints = {}

Expand Down Expand Up @@ -172,6 +175,14 @@ module.exports = class Reader extends Component {
const { preview } = this.els
const { facingMode } = this.props

// Stop stream if we've been asked to
if (this.shouldStop) {
const streamTrackToStop = stream.getTracks()[0]
setTimeout(() => {
streamTrackToStop.stop();
}, 2000);
}

// Preview element hasn't been rendered so wait for it.
if (!preview) {
return setTimeout(this.handleVideo, 200, stream)
Expand Down