mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-14 19:49:57 -05:00
Simplify setup method promise, add onError
This commit is contained in:
parent
89dd4720a4
commit
0180964333
1 changed files with 16 additions and 11 deletions
|
@ -98,7 +98,7 @@ class Video {
|
||||||
*/
|
*/
|
||||||
enableVideo () {
|
enableVideo () {
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
return this._setupVideo().then(() => this);
|
return this._setupVideo();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -222,15 +222,17 @@ class Video {
|
||||||
return this._singleSetup;
|
return this._singleSetup;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._video = document.createElement('video');
|
this._singleSetup = new Promise((resolve, reject) => {
|
||||||
const video = new Promise((resolve, reject) => {
|
|
||||||
navigator.getUserMedia({
|
navigator.getUserMedia({
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
width: {min: 480, ideal: 640},
|
width: {min: 480, ideal: 640},
|
||||||
height: {min: 360, ideal: 480}
|
height: {min: 360, ideal: 480}
|
||||||
}
|
}
|
||||||
}, stream => {
|
}, resolve, reject);
|
||||||
|
})
|
||||||
|
.then(stream => {
|
||||||
|
this._video = document.createElement('video');
|
||||||
this._video.src = window.URL.createObjectURL(stream);
|
this._video.src = window.URL.createObjectURL(stream);
|
||||||
// Hint to the stream that it should load. A standard way to do this
|
// Hint to the stream that it should load. A standard way to do this
|
||||||
// is add the video tag to the DOM. Since this extension wants to
|
// is add the video tag to the DOM. Since this extension wants to
|
||||||
|
@ -238,20 +240,23 @@ class Video {
|
||||||
// the webgl rendered Scratch canvas, another hint like this one is
|
// the webgl rendered Scratch canvas, another hint like this one is
|
||||||
// needed.
|
// needed.
|
||||||
this._track = stream.getTracks()[0];
|
this._track = stream.getTracks()[0];
|
||||||
resolve(this._video);
|
this._setupPreview();
|
||||||
}, err => {
|
return this;
|
||||||
// There are probably some error types we could handle gracefully here.
|
})
|
||||||
|
.catch(err => {
|
||||||
this._singleSetup = null;
|
this._singleSetup = null;
|
||||||
reject(err);
|
if (this.onError) {
|
||||||
|
this.onError(err);
|
||||||
|
} else {
|
||||||
|
log.error(`Unhandled io device error`, err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
this._singleSetup = video.then(() => this._setupPreview());
|
|
||||||
return this._singleSetup;
|
return this._singleSetup;
|
||||||
}
|
}
|
||||||
|
|
||||||
_disablePreview () {
|
_disablePreview () {
|
||||||
if (this._skin && this._drawable) {
|
if (this._skin) {
|
||||||
this._skin.clear();
|
this._skin.clear();
|
||||||
this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false});
|
this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue