Improving error handling in MediaManager

This commit is contained in:
David Négrier 2020-06-22 22:55:28 +02:00
parent 52df539314
commit 74af7d52c3

View File

@ -16,9 +16,9 @@ export class MediaManager {
audio: true, audio: true,
video: videoConstraint video: videoConstraint
}; };
updatedLocalStreamCallBack : Function; updatedLocalStreamCallBack : (media: MediaStream) => void;
constructor(updatedLocalStreamCallBack : Function) { constructor(updatedLocalStreamCallBack : (media: MediaStream) => void) {
this.updatedLocalStreamCallBack = updatedLocalStreamCallBack; this.updatedLocalStreamCallBack = updatedLocalStreamCallBack;
this.myCamVideo = this.getElementByIdOrFail<HTMLVideoElement>('myCamVideo'); this.myCamVideo = this.getElementByIdOrFail<HTMLVideoElement>('myCamVideo');
@ -63,7 +63,7 @@ export class MediaManager {
this.cinemaClose.style.display = "none"; this.cinemaClose.style.display = "none";
this.cinema.style.display = "block"; this.cinema.style.display = "block";
this.constraintsMedia.video = videoConstraint; this.constraintsMedia.video = videoConstraint;
this.getCamera().then((stream) => { this.getCamera().then((stream: MediaStream) => {
this.updatedLocalStreamCallBack(stream); this.updatedLocalStreamCallBack(stream);
}); });
} }
@ -107,8 +107,13 @@ export class MediaManager {
} }
//get camera //get camera
getCamera() { getCamera(): Promise<MediaStream> {
let promise = null; let promise = null;
if (navigator.mediaDevices === undefined) {
return Promise.reject<MediaStream>(new Error('Unable to access your camera or microphone. Your browser is too old (or you are running a development version of WorkAdventure on Firefox)'));
}
try { try {
promise = navigator.mediaDevices.getUserMedia(this.constraintsMedia) promise = navigator.mediaDevices.getUserMedia(this.constraintsMedia)
.then((stream: MediaStream) => { .then((stream: MediaStream) => {
@ -123,11 +128,12 @@ export class MediaManager {
return stream; return stream;
}).catch((err) => { }).catch((err) => {
console.info(`error get media {video: ${this.constraintsMedia.video}},{audio: ${this.constraintsMedia.audio}}`,err); console.info("error get media ", this.constraintsMedia.video, this.constraintsMedia.audio, err);
this.localStream = null; this.localStream = null;
throw err;
}); });
} catch (e) { } catch (e) {
promise = Promise.reject(false); promise = Promise.reject<MediaStream>(e);
} }
return promise; return promise;
} }