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

Don't restart video stream when turning flash off #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
104 changes: 52 additions & 52 deletions qr-scanner.legacy.min.js

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions qr-scanner.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion qr-scanner.min.js.map

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions qr-scanner.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion qr-scanner.umd.min.js.map

Large diffs are not rendered by default.

35 changes: 16 additions & 19 deletions src/qr-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,40 +292,37 @@ class QrScanner {
return this._flashOn;
}

async toggleFlash(): Promise<void> {
if (this._flashOn) {
await this.turnFlashOff();
} else {
await this.turnFlashOn();
}
}

async turnFlashOn(): Promise<void> {
if (this._flashOn || this._destroyed) return;
this._flashOn = true;
async turnFlashOnOff(onOff: boolean): Promise<void> {
if (this._flashOn == onOff || this._destroyed) return;
const oldFlashOn = this._flashOn
this._flashOn = onOff;
if (!this._active || this._paused) return; // flash will be turned on later on .start()
try {
if (!await this.hasFlash()) throw 'No flash available';
// Note that the video track is guaranteed to exist and to be a MediaStream due to the check in hasFlash
await (this.$video.srcObject as MediaStream).getVideoTracks()[0].applyConstraints({
// @ts-ignore: constraint 'torch' is unknown to ts
advanced: [{ torch: true }],
advanced: [{ torch: onOff}],
});
} catch (e) {
this._flashOn = false;
this._flashOn = oldFlashOn ;
throw e;
}
}

async toggleFlash(): Promise<void> {
await this.turnFlashOnOff(!this._flashOn);
}

async turnFlashOn(): Promise<void> {
await this.turnFlashOnOff(true);
}

async turnFlashOff(): Promise<void> {
if (!this._flashOn) return;
// applyConstraints with torch: false does not work to turn the flashlight off, as a stream's torch stays
// continuously on, see https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints#torch. Therefore,
// we have to stop the stream to turn the flashlight off.
this._flashOn = false;
await this._restartVideoStream();
await this.turnFlashOnOff(false);
}


destroy(): void {
this.$video.removeEventListener('loadedmetadata', this._onLoadedMetaData);
this.$video.removeEventListener('play', this._onPlay);
Expand Down
1 change: 1 addition & 0 deletions types/qr-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ declare class QrScanner {
constructor(video: HTMLVideoElement, onDecode: (result: string) => void, canvasSize?: number);
hasFlash(): Promise<boolean>;
isFlashOn(): boolean;
turnFlashOnOff(onOff: boolean): Promise<void>;
toggleFlash(): Promise<void>;
turnFlashOn(): Promise<void>;
turnFlashOff(): Promise<void>;
Expand Down