diff --git a/extensions/Lily/Video.js b/extensions/Lily/Video.js index f33c24fb0e..4f2c7565bd 100644 --- a/extensions/Lily/Video.js +++ b/extensions/Lily/Video.js @@ -31,6 +31,17 @@ elementContainer.ariaHidden = "true"; document.body.appendChild(elementContainer); + // Updated code to apply the project's volume to that of the video. + // This allows the volume to interact with Scratch Addons and Sound Expanded. + const updateAudio = function () { + for (const skin of renderer._allSkins) { + if (skin instanceof VideoSkin) { + let projectVolume = runtime.audioEngine.inputNode.gain.value; + skin.videoElement.volume = skin.videoVolume * projectVolume; + } + } + }; + const BitmapSkin = runtime.renderer.exports.BitmapSkin; class VideoSkin extends BitmapSkin { constructor(id, renderer, videoName, videoSrc) { @@ -42,6 +53,8 @@ /** @type {string} */ this.videoSrc = videoSrc; + this.videoVolume = 1; + this.videoError = false; this.readyPromise = new Promise((resolve) => { @@ -124,6 +137,7 @@ } dispose() { + runtime.off("AFTER_EXECUTE", updateAudio); super.dispose(); this.videoElement.pause(); this.videoElement.remove(); @@ -146,6 +160,8 @@ } }); + runtime.on("AFTER_EXECUTE", updateAudio); + runtime.on("RUNTIME_PAUSED", () => { for (const skin of renderer._allSkins) { if (skin instanceof VideoSkin) { @@ -598,7 +614,7 @@ case "duration": return videoSkin.videoElement.duration; case "volume": - return videoSkin.videoElement.volume * 100; + return videoSkin.videoVolume * 100; case "width": return videoSkin.size[0]; case "height": @@ -682,7 +698,8 @@ if (!videoSkin) return; const value = Cast.toNumber(args.VALUE); - videoSkin.videoElement.volume = Math.min(1, Math.max(0, value / 100)); + videoSkin.videoVolume = Math.min(1, Math.max(0, value / 100)); + updateAudio(); } setPlaybackRate(args) {