Skip to content

Commit 1d896dd

Browse files
committed
Update Hello.ts for webcam
1 parent cf10b3c commit 1d896dd

File tree

1 file changed

+24
-19
lines changed
  • src/VueJsTsWebcamAspNetCoreSample/src/components

1 file changed

+24
-19
lines changed

src/VueJsTsWebcamAspNetCoreSample/src/components/Hello.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class Hello extends Vue {
1313
photo: string = null;
1414
screenshotFormat: string = "image/jpeg";
1515

16-
private _refs: { [key: string] : Vue | Element | Vue[] | Element[] } = null;
16+
private _refs: any = null;
1717
private _video: HTMLVideoElement = null;
1818
private _stream: MediaStream = null;
1919
private _hasUserMedia: boolean = false;
@@ -31,7 +31,7 @@ export default class Hello extends Vue {
3131
this.photo = canvas.toDataURL(this.screenshotFormat);
3232
}
3333

34-
getCanvas (): HTMLCanvasElement {
34+
private getCanvas (): HTMLCanvasElement {
3535
if (!this._hasUserMedia) {
3636
return null;
3737
}
@@ -51,24 +51,29 @@ export default class Hello extends Vue {
5151
return null;
5252
}
5353

54+
private getMediaDevices (): MediaDevices {
55+
var md: MediaDevices = navigator.mediaDevices;
56+
if (md != null) {
57+
return md;
58+
}
59+
60+
md = new MediaDevices();
61+
return md;
62+
}
63+
5464
mounted (): void {
55-
this._refs = this.$refs;
56-
this._video = <HTMLVideoElement>this._refs["video"];
57-
58-
var n: any = <any>navigator;
59-
n.getUserMedia = n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia || n.msGetUserMedia || n.oGetUserMedia;
60-
61-
if (n.getUserMedia) {
62-
n.getUserMedia({ video: true },
63-
(stream) => {
64-
this.src = window.URL.createObjectURL(stream);
65-
this._stream = stream;
66-
this._hasUserMedia = true;
67-
},
68-
(error) => {
69-
console.log(error);
65+
this._refs = <any>this.$refs;
66+
this._video = <HTMLVideoElement>this._refs.video;
67+
68+
var md: MediaDevices = this.getMediaDevices();
69+
md.getUserMedia({ video: true })
70+
.then((stream) => {
71+
this.src = window.URL.createObjectURL(stream);
72+
this._stream = stream;
73+
this._hasUserMedia = true;
74+
}, (err) => {
75+
console.log(err);
7076
});
71-
}
7277
}
7378

7479
created (): void {
@@ -82,7 +87,7 @@ export default class Hello extends Vue {
8287

8388
beforeDestroy (): void {
8489
this._video.pause();
85-
this.src = "";
90+
this.src = null;
8691
this._stream.getTracks()[0].stop();
8792
}
8893

0 commit comments

Comments
 (0)