Skip to content

Lily/Video: Update volume functions to interact with the project volume #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
21 changes: 19 additions & 2 deletions extensions/Lily/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -42,6 +53,8 @@
/** @type {string} */
this.videoSrc = videoSrc;

this.videoVolume = 1;

this.videoError = false;

this.readyPromise = new Promise((resolve) => {
Expand Down Expand Up @@ -124,6 +137,7 @@
}

dispose() {
runtime.off("AFTER_EXECUTE", updateAudio);
super.dispose();
this.videoElement.pause();
this.videoElement.remove();
Expand All @@ -146,6 +160,8 @@
}
});

runtime.on("AFTER_EXECUTE", updateAudio);

runtime.on("RUNTIME_PAUSED", () => {
for (const skin of renderer._allSkins) {
if (skin instanceof VideoSkin) {
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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) {
Expand Down
Loading