Parcourir la source

CameraSource: handle the Promise returned by video.play()

customisations
alemart il y a 1 an
Parent
révision
5be7e62b18
1 fichiers modifiés avec 23 ajouts et 6 suppressions
  1. 23
    6
      src/sources/camera-source.ts

+ 23
- 6
src/sources/camera-source.ts Voir le fichier

@@ -25,7 +25,7 @@ import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
25 25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26 26
 import { Utils } from '../utils/utils';
27 27
 import { Resolution } from '../core/resolution';
28
-import { NotSupportedError, AccessDeniedError } from '../utils/errors';
28
+import { NotSupportedError, AccessDeniedError, IllegalOperationError } from '../utils/errors';
29 29
 import { VideoSource } from './video-source';
30 30
 
31 31
 
@@ -116,18 +116,35 @@ export class CameraSource extends VideoSource
116 116
             navigator.mediaDevices.getUserMedia(constraints).then(stream => {
117 117
                 const video = this._cameraVideo;
118 118
                 video.onloadedmetadata = () => {
119
-                    video.play();
120
-                    Utils.log('Access to the webcam has been granted.');
121
-                    resolve(video);
119
+                    const promise = video.play();
120
+                    const success = 'Access to the webcam has been granted.';
121
+
122
+                    // handle older browsers
123
+                    if(promise === undefined) {
124
+                        Utils.log(success);
125
+                        resolve(video);
126
+                        return;
127
+                    }
128
+
129
+                    // handle promise
130
+                    promise.then(() => {
131
+                        Utils.log(success);
132
+                        resolve(video);
133
+                    }).catch(error => {
134
+                        reject(new IllegalOperationError(
135
+                            'Webcam error!',
136
+                            error
137
+                        ));
138
+                    });
122 139
                 };
123 140
                 video.setAttribute('playsinline', '');
124 141
                 video.setAttribute('autoplay', '');
125 142
                 video.setAttribute('muted', '');
126 143
                 video.srcObject = stream;
127
-            }).catch(err => {
144
+            }).catch(error => {
128 145
                 reject(new AccessDeniedError(
129 146
                     'Please give access to the webcam and reload the page.',
130
-                    err
147
+                    error
131 148
                 ));
132 149
             });
133 150
         }).then(_ => super._init()); // this will call VideoSource._handleBrowserQuirks()

Chargement…
Annuler
Enregistrer