- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
import { videoList } from "../cache.js";
import { fetchData } from "./fetchdata.js";
import { hasVideoFormat } from "../../system/device.js";
import * as fileUtil from "../../utils/file.js";
import { crossOrigin } from "../settings.js";
import { isDataUrl } from "./../../utils/string.js";
/**
* parse/preload a Video file
* @param {loader.Asset} data - asset data
* @param {Function} [onload] - function to be called when the asset is loaded
* @param {Function} [onerror] - function to be called in case of error
* @returns {number} the amount of corresponding resource parsed/preloaded
* @ignore
*/
export function preloadVideo(data, onload, onerror) {
if (typeof videoList[data.name] !== "undefined") {
// Video already preloaded
return 0;
}
let videoElement = videoList[data.name] = document.createElement("video");
if (isDataUrl(data.src)) {
const mimeType = data.src.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0];
if (!mimeType || videoElement.canPlayType(mimeType) === "") {
throw new Error(`Invalid dataURL or Video file format not supported: ${mimeType}`);
}
} else {
if (!hasVideoFormat(fileUtil.getExtension(data.src))) {
throw new Error(`Video file format not supported: ${fileUtil.getExtension(data.src)}`);
}
}
if (isDataUrl(data.src)) {
fetchData(data.src, "blob")
.then(blob => {
videoElement.src = globalThis.URL.createObjectURL(blob);
})
.catch(error => {
if (typeof onerror === "function") {
onerror(error);
}
});
} else {
// just a url path
videoElement.src = data.src;
}
videoElement.setAttribute("preload", data.stream === true ? "metadata" : "auto");
videoElement.setAttribute("playsinline", "true");
videoElement.setAttribute("disablePictureInPicture", "true");
videoElement.setAttribute("controls", "false");
videoElement.setAttribute("crossorigin", crossOrigin);
if (data.autoplay === true) {
videoElement.setAttribute("autoplay", "true");
}
if (data.loop === true) {
videoElement.setAttribute("loop", "true");
}
if (typeof onload === "function") {
if (data.stream === true) {
videoElement.onloadedmetadata = () => {
if (typeof onload === "function") {
onload();
}
};
} else {
videoElement.oncanplay = () => {
if (typeof onload === "function") {
onload();
}
};
}
}
if (typeof onerror === "function") {
videoElement.onerror = () => {
onerror();
};
}
videoElement.load();
return 1;
}