forked from basst314/ngx-webcam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent double execution enumerateDevices method which breaks mobile …
…chrome and safari Fix basst314#95
- Loading branch information
Showing
2 changed files
with
27 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
export class WebcamUtil { | ||
|
||
private static availableVideoInputs: MediaDeviceInfo[] = []; | ||
|
||
/** | ||
* Lists available videoInput devices | ||
* @returns a list of media device info. | ||
*/ | ||
public static getAvailableVideoInputs(): Promise<MediaDeviceInfo[]> { | ||
public static async getAvailableVideoInputs(): Promise<MediaDeviceInfo[]> { | ||
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) { | ||
return Promise.reject('enumerateDevices() not supported.'); | ||
throw new Error('enumerateDevices() not supported.'); | ||
} | ||
|
||
if (!WebcamUtil.availableVideoInputs.length) { | ||
const devices = await navigator.mediaDevices.enumerateDevices(); | ||
WebcamUtil.availableVideoInputs = devices.filter((device: MediaDeviceInfo) => device.kind === 'videoinput'); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
navigator.mediaDevices.enumerateDevices() | ||
.then((devices: MediaDeviceInfo[]) => { | ||
resolve(devices.filter((device: MediaDeviceInfo) => device.kind === 'videoinput')); | ||
}) | ||
.catch(err => { | ||
reject(err.message || err); | ||
}); | ||
}); | ||
return WebcamUtil.availableVideoInputs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters